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

**URL:** <https://discuss.elastic.co/t/get-a-count-of-buckets-from-a-terms-agg-but-not-the-buckets-themselves/286259>\
**Category:** Elasticsearch\
**Created:** [October 8, 2021, 2:59pm UTC](https://discuss.elastic.co/t/get-a-count-of-buckets-from-a-terms-agg-but-not-the-buckets-themselves/286259 "2021-10-08T14:59:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![annmarie-switzer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/annmarie-switzer/32/82870_2.png) [@annmarie-switzer](https://discuss.elastic.co/u/annmarie-switzer)\
**Post date:** [October 8, 2021, 2:59pm UTC](https://discuss.elastic.co/t/get-a-count-of-buckets-from-a-terms-agg-but-not-the-buckets-themselves/286259/1 "2021-10-08T14:59:06Z")

</div>

### 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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_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.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 10, 2021, 7:58am UTC](https://discuss.elastic.co/t/get-a-count-of-buckets-from-a-terms-agg-but-not-the-buckets-themselves/286259/2 "2021-10-10T07:58:40Z")

</div>

Check out the cardinality aggregation.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 7, 2021, 7:59am UTC](https://discuss.elastic.co/t/get-a-count-of-buckets-from-a-terms-agg-but-not-the-buckets-themselves/286259/3 "2021-11-07T07:59:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
