如何过滤由嵌套聚合产生的doc_count值

时间:2019-03-14 13:17:45

标签: elasticsearch elasticsearch-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的存储桶中只有一个聚合结果。有人可以帮我做这个过滤器吗?请让我知道是否需要其他信息。

1 个答案:

答案 0 :(得分:1)

仔细查看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"
               }
            }
         }
      }
   }
}

希望有帮助!

相关问题