Sorting 按与文档相同的属性对Elasticsearch Bucket进行排序
有一种方法可以按多个属性对文档进行排序。 我想按相同的属性以相同的方式对聚合的bucket进行排序。这可能吗 以下是我的示例文档:Sorting 按与文档相同的属性对Elasticsearch Bucket进行排序,sorting,elasticsearch,aggregation,Sorting,elasticsearch,Aggregation,有一种方法可以按多个属性对文档进行排序。 我想按相同的属性以相同的方式对聚合的bucket进行排序。这可能吗 以下是我的示例文档: curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d' { "index" : { "_index" : "cars", "_type" : "cars", "_id" : "1" } } { "make":"BMW", "serie
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设置为true,以便能够聚合:
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" }
}
}
}
}
}
'
你知道如何按make和series对bucket进行排序吗?首先,你可能想指定
make
和series
为类型,而不是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
}
]
}
}
]
我想在我的查询中添加一种
sort
参数,比如在对普通文档进行排序时:“sort”:[{“series”:“desc”},{“make”:“asc”}]
,这样订单桶将首先按序列排序,然后按make排序