【问题标题】:How can I know if two different aggregations aggregated the same docs?我如何知道两个不同的聚合是否聚合了相同的文档?
【发布时间】:2019-05-15 17:21:38
【问题描述】:

假设我有两个 aggs:

   GET .../_search
   {
       "size": 0,
       "aggs": {
           "foo": {
               "terms": {
                   "field": "foo"
               }
           },
           "bar": {
               "terms": {
                   "field": "bar"
               }
           }
       }
   }

返回以下内容:

   ...
   "aggregations": {
       "foo": {
           "doc_count_error_upper_bound": 0,
           "sum_other_doc_count": 0,
           "buckets": [
               {
                   "key": "Africa",
                   "doc_count": 23
               }
           ]
       },
       "bar": {
           "doc_count_error_upper_bound": 0,
           "sum_other_doc_count": 0,
           "buckets": [
               {
                   "key": "Oil",
                   "doc_count": 23
               }
           ]
       }
   }

我的问题是,我怎么知道“foo”和“bar”聚合是否聚合了相同的 23 个文档?

我尝试在 "foo" 和 "bar" aggs 中添加一个子 agg 来对任意数字字段求和,但这并不是万无一失的。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以添加聚合文档的身份字段的子聚合,您可以使用术语或复合聚合来执行此操作。使用术语时,您需要提供大小。看这个例子:

    GET .../_search
       {
           "size": 0,
           "aggs": {
               "foo": {
                   "terms": {
                       "field": "foo"
                   },
                   "aggs" : {
                       "terms" : {
                           "field" : your_id_here
                       }
                   }
               },
               "bar": {
                   "terms": {
                       "field": "bar"
                   },
                   "aggs" : {
                       "terms" : {
                           "field" : your_id_here
                       }
                   }
               }
           }
       }
    

    然后您需要比较嵌套聚合。

    另一种方法是使用搜索查询过滤掉所需的文档。

    【讨论】:

    • 谢谢,但是这里有两个问题。首先,如果文档数量很大怎么办?大桶大小要么是不可能的,要么是极其浪费的。其次,比较每个文档 ID 真的很慢。必须有一种优雅的方式来做到这一点。例如,假设有一个 xor agg,然后我可以在 doc_ids 上使用它。
    猜你喜欢
    • 2020-08-31
    • 2022-09-23
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多