# How to get exact number of buckets returned with too\_many\_buckets\_exception

**URL:** <https://discuss.elastic.co/t/how-to-get-exact-number-of-buckets-returned-with-too-many-buckets-exception/214284>\
**Category:** Elasticsearch\
**Created:** [January 8, 2020, 5:08pm UTC](https://discuss.elastic.co/t/how-to-get-exact-number-of-buckets-returned-with-too-many-buckets-exception/214284 "2020-01-08T17:08:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrianJenney](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brianjenney/32/60496_2.png) [@BrianJenney](https://discuss.elastic.co/u/BrianJenney)\
**Post date:** [January 8, 2020, 5:08pm UTC](https://discuss.elastic.co/t/how-to-get-exact-number-of-buckets-returned-with-too-many-buckets-exception/214284/1 "2020-01-08T17:08:58Z")

</div>

Like many people - I've received the `too_many_buckets_exception` error with an unoptimized query. How can we get the actual number of buckets returned that triggered this error?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 9, 2020, 3:36pm UTC](https://discuss.elastic.co/t/how-to-get-exact-number-of-buckets-returned-with-too-many-buckets-exception/214284/2 "2020-01-09T15:36:07Z")

</div>

the total buckets are not calculated, but the execution is aborted if that limit is exceeded.

you could maybe use a [cardinality aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-aggregations-metrics-cardinality-aggregation.html) to get a an estimate how many buckets would be created by changing your query

---

<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:** [February 6, 2020, 3:36pm UTC](https://discuss.elastic.co/t/how-to-get-exact-number-of-buckets-returned-with-too-many-buckets-exception/214284/3 "2020-02-06T15:36:16Z")

</div>

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