2017-06-16 69 views
0

可以通過多個屬性對文檔進行排序。 我想通過相同的屬性以相同的方式對聚合桶進行排序。這可能嗎?對與文檔具有相同屬性的Elasticsearch存儲桶進行排序

這裏是我的示例文件:

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} 
    ' 

這裏我設置fielddata到真正能夠聚合:

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 
    } 
    } 
} 
' 

這裏是查詢聚合桶:

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" } 
     } 
     } 
    } 
    } 
} 
' 

任何想法如何按品牌和系列來分類桶?

回答

0

首先,你可能想指定makeserieskeyword型,而不是text的,因爲它並不需要分析(您想確切的這些字段匹配)。

你的輸出是什麼樣的?當我嘗試它時,它似乎被正確排序。

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

我要添加到我的查詢類型的'sort'參數,當我整理正常文檔,如:'「排序」:[{ 「系列」:「遞減」},{ 「做」 :「asc」} ]',這樣訂單存儲桶將按照系列先排序, –

相關問題