2017-05-28 107 views
1

我有一個MySQL查詢像這樣:如何在elasticsearch中按字段進行排序?

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
WHERE 1=1 
AND wp_posts.ID IN (2991,2993,2989,2983,2987) 
AND wp_posts.post_type = 'product' 
AND ((wp_posts.post_status = 'publish')) 
ORDER BY FIELD(wp_posts.ID, 2991,2993,2989,2983,2987) 
LIMIT 0, 10 

而且我要指出的具體線路:
ORDER BY FIELD(wp_posts.ID, 2991,2993,2989,2983,2987)

在elasticsearchwe只能有排序順序由ascdeschttps://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_order

所以任何人都可以指出我如何在彈性搜索查詢中實現order by field (id, x,y,z)

回答

1

這不像SQL那麼容易,但是您可以使用function_score和多個match es來表示您的值。
這樣的事情:

{ 
    "query": { 
     "function_score": { 
     "query": { 
      "bool": { 
       "must": [ 
        { 
        "terms": { 
         " ID": [ 
          2991, 
          2993, 
          2989, 
          2983, 
          2987 
         ] 
        } 
        }, 
        { 
        "term": { 
         "post_type": { 
          "value": "product" 
         } 
        } 
        }, 
        { 
        "term": { 
         "post_status": { 
          "value": "publish" 
         } 
        } 
        } 
       ] 
      } 
     }, 
     "boost": "5", 
     "functions": [ 
      { 
       "filter": { 
        "match": { 
        "ID": 2991 
        } 
       }, 
       "weight": 100 
      }, 
      { 
       "filter": { 
        "match": { 
        "test": 2993 
        } 
       }, 
       "weight": 99 
      }, 
      { 
       "filter": { 
        "match": { 
        "test": 2989 
        } 
       }, 
       "weight": 98 
      }, 
      { 
       "filter": { 
        "match": { 
        "test": 2983 
        } 
       }, 
       "weight": 97 
      }, 
      { 
       "filter": { 
        "match": { 
        "test": 2987 
        } 
       }, 
       "weight": 96 
      } 
     ], 
     "score_mode": "max", 
     "boost_mode": "multiply" 
     } 
    } 
} 
+0

是的,它的工作。但它確實使查詢變得複雜。無論如何,非常感謝你幫助我。 – Ivan

相關問題