【发布时间】:2016-09-08 05:37:29
【问题描述】:
我如何对键上的弹性搜索聚合存储桶进行排序。我有嵌套聚合,想对我的第二个聚合桶结果进行排序。
像我一样:
"result": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 20309,
"doc_count": 752,
"Events": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "impression",
"doc_count": 30
},
{
"key": "page_view",
"doc_count": 10
},
...
]
}
},
{
"key": 20771,
"doc_count": 46,
"Events": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "impression",
"doc_count": 32
},
{
"key": "page_view",
"doc_count": 9
},
...
]
}
},
我希望我的 Events 聚合存储桶按键 impression 或 page_view 上的 desc/asc 排序。
我如何获得这样的结果集?
这是我的查询
GET someindex/useractivity/_search?search_type=count
{
"size": 1000000,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"range": {
"created_on": {
"from": "2015-01-12",
"to": "2016-05-12"
}
}
},
{
"term": {
"group_id": 1
}
}
]
}
}
}
},
"aggs": {
"result": {
"terms": {
"field": "entity_id",
"size": 1000000
},
"aggs": {
"Events": {
"terms": {
"field": "event_type",
"min_doc_count": 0,
"size": 10
}
}
}
}
}
}
我尝试过使用 _key,但它在存储桶中排序。我想通过查看所有存储桶进行排序。就像我有一把钥匙impression。我希望我的存储桶结果使用此键进行排序。不在桶内。
我希望我的结果集类似于如果我想按降序对impression 进行排序,那么我的结果应该是
"buckets": [
{
"key": 20771,
"doc_count": 46,
"Events": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "impression",
"doc_count": 32
},
{
"key": "page_view",
"doc_count": 9
},
...
]
}
},
{
"key": 20309,
"doc_count": 752,
"Events": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "impression",
"doc_count": 30
},
{
"key": "page_view",
"doc_count": 10
},
...
]
}
},
即展示次数最多的存储桶应位于顶部。 (按展示次数降序排列存储桶)
【问题讨论】:
-
请分享您正在运行的查询。
-
通过使用
"order": { "_key" : "asc" }还是我错过了什么? -
使用
_key将在桶内排序。我想用所有的桶对它进行排序。就像我有一把钥匙impression。我希望我的存储桶结果使用此键进行排序。不在桶内 -
您能否更改该聚合的输出并显示所需的结果?
-
@Andrei Stefan 我更新了我的问题
标签: sorting elasticsearch aggregation bucket-sort