elasticsearch:聚合期间按不同计数过滤

lx0bsm1f  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(330)

以下查询按ID的最大非重复计数顺序返回非重复ID。我想做的是“只包括文档总数小于2000的ID”

{
  "size": "0",
  "query": {
    "range": {
      "@timestamp": {
        "gte": "2020-10-20T00:00:00",
        "lt": "2020-10-21T00:00:00"
      }
    }
  },
  "aggs": {
    "ids": {
      "terms": {
        "field": "Id.keyword",
        "size": 1000
      }
    }
  }
}

我试着按“doc\u count”添加过滤器,但没有用。我该怎么做?

gmxoilav

gmxoilav1#

您可以使用bucket选择器聚合来过滤bucket
bucket selector aggregation是一个父管道聚合,它执行一个脚本,该脚本确定当前bucket是否将保留在父多bucket聚合中。

{
  "size": "0",
  "query": {
    "range": {
      "@timestamp": {
        "gte": "2020-10-20T00:00:00",
        "lt": "2020-10-21T00:00:00"
      }
    }
  },
  "aggs": {
    "ids": {
      "terms": {
        "field": "Id.keyword",
        "size": 1000
      },
      "aggs": {
        "count_filter": {
          "bucket_selector": {
            "buckets_path": {
              "values": "_count"
            },
            "script": "params.values < 2000"   <-- note this
          }
        }
      }
    }
  }
}

相关问题