如何获得具有大小集的聚合中唯一术语的总数?

How to get the total count of unique terms on aggregations with the size set?

使用Terms Aggregation on an ElasticSearch query, the result will limit the buckets to the top 10 items or the value set on the size参数时。例如:

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

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

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

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

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

"unique_cities": {
  "value": 73
}

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