【问题标题】:How to merge aggregation bucket in Elasticsearch?如何在 Elasticsearch 中合并聚合桶?
【发布时间】:2022-01-04 05:21:50
【问题描述】:

查询

GET /_search
{
  "size" : 0,
  "query" : {
    "ids" : {
      "types" : [ ],
      "values" : [ "someId1", "someId2", "someId3" ... ]
    }
  },
  "aggregations" : {
    "how_to_merge" : {
      "terms" : {
        "field" : "country",
        "size" : 50
      }
    }
  }
}

结果

{
   ...
   "aggregations": {
      "how_to_merge": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "KR",
               "doc_count": 90
            },
            {
               "key": "JP",
               "doc_count": 83
            },
            {
               "key": "US",
               "doc_count": 50
            },
            {
               "key": "BE",
               "doc_count": 9
            }
         ]
      }
   }
}

我想合并“KR”和“JP”和“US”

并将键名更改为“NEW_RESULT”

所以结果一定是这样的:

{
   ...
   "aggregations": {
      "how_to_merge": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "NEW_RESULT",
               "doc_count": 223
            },
            {
               "key": "BE",
               "doc_count": 9
            }
         ]
      }
   }
}

在elasticsearch查询中可以吗?

我不能使用客户端解决方案,因为实体太多,检索所有实体和合并对于我的应用程序来说可能太慢了。

感谢您的帮助和cmets!

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-dsl


    【解决方案1】:

    您可以尝试为此编写script,但我建议将此方法与客户端处理进行基准测试,因为它可能会很慢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-06
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      • 2021-07-26
      相关资源
      最近更新 更多