2017-06-16 4 views
0

Es gibt eine Möglichkeit, Dokumente nach mehreren Attributen zu sortieren. Ich möchte aggregierte Buckets auf die gleiche Weise nach denselben Attributen sortieren. Ist das möglich?Elasticsearch-Buckets nach denselben Attributen wie Dokumente sortieren

Hier sind meine Beispieldokumente:

curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: 
application/json' -d' 
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "1" } } 
    { "make":"BMW", "series":"3er", "doorCount": 4} 
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "2" } } 
    { "make":"BMW", "series":"3er", "doorCount": 5} 
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "3" } } 
    { "make":"Opel", "series":"Astra", "doorCount": 2} 
    { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "4" } } 
    { "make":"Opel", "series":"Omega", "doorCount": 2} 
    ' 

Here I fielddata auf true bin Einstellung der Lage sein, zu aggregieren:

curl -XPUT 'localhost:9200/cars/_mapping/cars?pretty' -H 'Content-Type: application/json' -d' 
{ 
    "properties": { 
    "make": { 
     "type":  "text", 
     "fielddata": true 
    }, 
    "series": { 
     "type":  "text", 
     "fielddata": true 
    } 
    } 
} 
' 

Hier ist die Abfrage aggregiert Eimer:

curl -XGET 'localhost:9200/cars/cars/_search?pretty' -d' 
{ 
    "size": 0, 
    "aggregations" : { 
    "CARS" : { 
     "terms" : { 
     "script" : { 
      "inline" : "doc[\"make\"].value + \"_\" + doc[\"series\"].value", 
      "lang" : "painless" 
     }  
     }, 
     "aggregations" : { 
     "make" : { 
      "terms" : { "field" : "make"} 
     }, 
     "series" : { 
      "terms" : { "field" : "series" } 
     } 
     } 
    } 
    } 
} 
' 

Haben Sie eine Idee, wie Sie Eimer nach Marke und Serie sortieren?

Antwort

0

First off, möchten Sie wahrscheinlich make und series angeben Typ sein keyword statt text, weil es nicht analysiert werden muss (Sie passt genau auf diesen Feldern).

Wie sieht Ihre Ausgabe aus? Als ich es versuchte, scheint es richtig sortiert zu sein.

[ 
    { 
     "key": "bmw_3er", 
     "doc_count": 2, 
     "series": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
      "key": "3er", 
      "doc_count": 2 
      } 
     ] 
     }, 
     "make": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
      "key": "bmw", 
      "doc_count": 2 
      } 
     ] 
     } 
    }, 
    { 
     "key": "opel_astra", 
     "doc_count": 1, 
     "series": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
      "key": "astra", 
      "doc_count": 1 
      } 
     ] 
     }, 
     "make": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
      "key": "opel", 
      "doc_count": 1 
      } 
     ] 
     } 
    }, 
    { 
     "key": "opel_omega", 
     "doc_count": 1, 
     "series": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
      "key": "omega", 
      "doc_count": 1 
      } 
     ] 
     }, 
     "make": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
      "key": "opel", 
      "doc_count": 1 
      } 
     ] 
     } 
    } 
    ] 
+0

Ich möchte wie zu meiner Anfrage Art von 'sort' Parameter hinzuzufügen, wenn ich normale Dokumente bin Sortierung:' "sort": [ { "Serie": "ab"}, { "make" : "asc"} ] ', so dass die Auftrags-Buckets zuerst nach Serien sortiert und dann nach Sekunden sortiert werden –

Verwandte Themen