Get a count of buckets from a terms agg, but not the buckets themselves

Is there any way to get the bucket count from a terms agg, but just the count, not the buckets themselves?


I have a scenario where I want to display a table (with pagination) of all the non-unique values (buckets) for a given field, hash.

I have millions of documents and potentially thousands, even tens of thousands, of them will have a non-unique hash field.

Because I have so many documents but I also have a requirement for a UI with pagination features, I want to use a terms query with partitions. This allows for something like server-side pagination, and lets the user go backwards and forwards through results.

The problem is that I am unable to determine the total number of buckets ahead of time. This is problematic from a UI perspective, where the user of the table wants to be able to see something like "Showing 50-100 of 1000 results". That "1000" number is the total number of buckets which I will not have on page load.

I do know how to get the total number of non-unique documents. That value is fairly simple to calculate using the count API and a cardinality query. But I do not want the number of documents, I want the number of buckets.

I am also aware of the Stats Bucket Aggregation, but that also does not work for me because it counts only the number of returned buckets, not all the possible buckets.

I don't want to use the terms query without partitions, because as I said before, I could be requesting tens of thousands of buckets over the wire and into the browser, which would cause performance issues.

I have also considered the Composite Aggregation, but that does not work because the results can't be pre-sorted or filtered, nor would the user be able to go backwards through results.

Check out the cardinality aggregation.

1 Like