# Limiting the number of documents for each bucket in term aggregation

**URL:** https://discuss.elastic.co/t/limiting-the-number-of-documents-for-each-bucket-in-term-aggregation/281818
**Category:** Elasticsearch
**Created:** [August 18, 2021, 12:39pm UTC](https://discuss.elastic.co/t/limiting-the-number-of-documents-for-each-bucket-in-term-aggregation/281818 "2021-08-18T12:39:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![menshenin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/menshenin/32/52975_2.png) [@menshenin](https://discuss.elastic.co/u/menshenin)
#### Post date: [August 18, 2021, 12:39pm UTC](https://discuss.elastic.co/t/limiting-the-number-of-documents-for-each-bucket-in-term-aggregation/281818/1 "2021-08-18T12:39:41Z")

</div>

Hi,

There is a set of terms. I want to know which ones are present in the search results and I use term aggregation for this purpose.

Is there some way to limit the number of documents for each bucket in term aggregation in order to improve performance?  
All I want to know is if there is at least one document containing term. I don't need to count the number of documents for each term.

Or maybe there is another more efficient way to solve this problem?  
Thank you.

---

<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: [August 25, 2021, 10:14am UTC](https://discuss.elastic.co/t/limiting-the-number-of-documents-for-each-bucket-in-term-aggregation/281818/2 "2021-08-25T10:14:11Z")

</div>

Couldnt this be simply a search/count operation in combination with `terminate_after` to speed it up for each term? See [Search your data | Elasticsearch Guide [7.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-your-data.html#quickly-check-for-matching-docs)

---

<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: [August 25, 2021, 10:37am UTC](https://discuss.elastic.co/t/limiting-the-number-of-documents-for-each-bucket-in-term-aggregation/281818/3 "2021-08-25T10:37:34Z")

</div>

What's the business question you're trying to answer with this request?

If your goal is to count the number of unique terms, use the `cardinality` aggregation.

If you want the most popular terms from a large set of unique terms - just faster - then skipping counting some terms may just lead to inaccuracies as to what are the correct subset of terms to select for the final result.

If the number of unique terms in the index is small (so you always return the full set rather than just the top N) we'd need to terminate the search's collection of docs only after a defined number of terms had been discovered - but there's no way for you to provide what that expected number is (or perhaps for you to know what to expect). There's no way for us to know that there's not an extra term to be found at the end of the very long stream of docs that match a query so it's hard to add a shortcut.

---

<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: [September 22, 2021, 10:38am UTC](https://discuss.elastic.co/t/limiting-the-number-of-documents-for-each-bucket-in-term-aggregation/281818/4 "2021-09-22T10:38:01Z")

</div>

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