2017-05-28 3 views
1

Ich habe eine MySQL-Abfrage wie folgt:Sortierung nach Feld in 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 

Und ich möchte die spezifische Linie beachten:
ORDER BY FIELD(wp_posts.ID, 2991,2993,2989,2983,2987)

In elasticsearchwe kann nur haben Sortierreihenfolge von asc oder desc: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_sort_order

Also kann mir jemand auf die order by field (id, x,y,z) in elasticsearch Abfrage zu erreichen?

Antwort

1

Es ist nicht so einfach wie SQL, aber Sie können function_score und mehrere match es für Ihre Werte verwenden.
etwas wie folgt aus:

{ 
    "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

Ja, es funktioniert. Aber es macht die Abfrage zu ES kompliziert. Wie auch immer, vielen Dank, dass Sie mir geholfen haben. – Ivan

Verwandte Themen