When using the Terms Aggregation on an ElasticSearch query, the result will limit the buckets to the top 10 items or the value set on the size parameter. For example:
{
"aggs" : {
"cities" : {
"terms" : {
"field" : "city",
"size": 20
}
}
}
}
This query would give me the top 20 buckets and their counts. How do I change this query to know the total count of unique "city"
terms, so I can present something like "showing the top 20 cities of 73"?
The Cardinality Aggregation can be requested on the same query. So on the provided example, we would have:
{
"aggs" : {
"cities" : {
"terms" : {
"field" : "city",
"size": 20
}
},
"unique_cities": {
"cardinality": {
"field": "city"
}
}
}
}
And the "aggregations"
response would have, besides the "cities"
element (which contains the buckets
), the "unique_cities"
element with the cardinality:
"unique_cities": {
"value": 73
}
Credits to this issue on github: Return number of buckets for terms aggregation