【问题标题】:elasticsearch aggregations sort on buckets keyselasticsearch 聚合对存储桶键进行排序
【发布时间】: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 聚合存储桶按键 impressionpage_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


【解决方案1】:

试试这个聚合:

{
  "size": 0,
  "aggs": {
    "result": {
      "terms": {
        "field": "entity_id",
        "size": 10,
        "order": {
          "impression_Events": "desc"
        }
      },
      "aggs": {
        "Events": {
          "terms": {
            "field": "event_type",
            "min_doc_count": 0,
            "size": 10
          }
        },
        "impression_Events": {
          "filter": {
            "term": {
              "event_type": "impression"
            }
          }
        }
      }
    }
  }
}

【讨论】:

  • 我试过了,但它不能按我的需要工作。我注意到你来自@elastic,如果我没记错的话。那么有什么方法可以实现这样的结果集吗?
  • 你能解释一下什么不是你所期望的吗?
  • 我已经更新了我的问题并显示了想要的结果
  • 我在问为什么我的查询不适合你。请详细说明!
猜你喜欢
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2019-09-15
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
  • 2021-05-06
相关资源
最近更新 更多