【问题标题】:How to get the total count of unique terms on aggregations with the size set?如何使用大小集获取聚合中唯一术语的总数?
【发布时间】:2023-03-17 12:21:02
【问题描述】:

在 ElasticSearch 查询中使用 Terms Aggregation 时,结果会将存储桶限制为前 10 项或在 size 参数上设置的值。例如:

{
  "aggs" : {
    "cities" : {
      "terms" : { 
        "field" : "city",
        "size": 20
      }
    }
  }
}

此查询将为我提供前 20 个存储桶及其计数。如何更改此查询以了解唯一 "city" 术语的总数,以便我可以呈现类似“显示 73 个排名前 20 的城市”之类的内容?

【问题讨论】:

  • 虽然可能不是很明显,但terms aggregations 文档的Filtering Values with partitions 部分可能会回答这个问题,其中说 “使用基数聚合来估计唯一 account_id 的总数值”.

标签: elasticsearch unique elasticsearch-aggregation


【解决方案1】:

Cardinality Aggregation 可以在同一个查询中请求。因此,在提供的示例中,我们将:

{
  "aggs" : {
    "cities" : {
      "terms" : { 
        "field" : "city",
        "size": 20
      }
    },
    "unique_cities": {
      "cardinality": {
        "field": "city"
      }
    }
  }
}

除了"cities" 元素(其中包含buckets)之外,"aggregations" 响应还包含具有基数的"unique_cities" 元素:

"unique_cities": {
  "value": 73
}

此问题在 github 上的致谢: Return number of buckets for terms aggregation

【讨论】:

  • 基数聚合将返回近似数字。所以结果将是近似值而不是精确计数。仅供参考。
猜你喜欢
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
相关资源
最近更新 更多