如何过滤嵌套聚合结果的 doc_count 值

How can I filter doc_count value which is a result of a nested aggregation

如何过滤嵌套聚合结果的 doc_count 值? 这是我的查询:

    "aggs": {
            "CDIDs": {
              "terms": {
                "field": "CDID.keyword",
                "size": 1000
              },
              "aggs": {
                "my_filter": {
                  "filter": {
                    "range": {
                      "transactionDate": {
                        "gte": "now-1M/M"
                      }
                    }
                  }
                },
                "in_active": {
                  "bucket_selector": {
                    "buckets_path": {
                      "doc_count": "_count"
                    },
                    "script": "params.doc_count > 4"
                  }
                }
              }
            }
          }

查询结果如下:

      {
          "aggregations" : {
            "CDIDs" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 2386,
              "buckets" : [
                {
                  "key" : "1234567",
                  "doc_count" : 5,
                  "my_filter" : {
                    "doc_count" : 4
                  }
                },
                {
                  "key" : "12345",
                  "doc_count" : 5,
                  "my_filter" : {
                    "doc_count" : 5
                  }
                }
              ]
            }
          }
        }

我正在尝试过滤此处的第二个 doc_count 值。假设我想要有 > 4 的文档,所以结果应该是 doc_count = 5 的桶中只有一个聚合结果。任何人都可以帮助我如何做这个过滤器?如果需要任何其他信息,请告诉我。

仔细查看 bucket_selector 聚合。您只需在 buckets_path 部分中指定聚合名称,即 "doc_count":"my_filter>_count"

管道聚合 (buckets_path) 有自己的语法,其中 > 作为分隔符。有关这方面的更多信息,请参阅此 LINK

聚合查询

POST <your_index_name>/_search
{  
   "size":0,
   "aggs":{  
      "CDIDs":{  
         "terms":{  
            "field":"CDID.keyword",
            "size":1000
         },
         "aggs":{  
            "my_filter":{  
               "filter":{  
                  "range":{  
                     "transactionDate":{  
                        "gte":"now-1M/M"
                     }
                  }
               }
            },
            "in_active":{  
               "bucket_selector":{  
                  "buckets_path":{  
                     "doc_count":"my_filter>_count"
                  },
                  "script":"params.doc_count > 4"
               }
            }
         }
      }
   }
}

希望对您有所帮助!