# Elasticsearch term aggregations : use case for a single field

**URL:** https://discuss.elastic.co/t/elasticsearch-term-aggregations-use-case-for-a-single-field/240672
**Category:** Elasticsearch
**Created:** [July 10, 2020, 9:58am UTC](https://discuss.elastic.co/t/elasticsearch-term-aggregations-use-case-for-a-single-field/240672 "2020-07-10T09:58:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vic22](https://avatars.discourse-cdn.com/v4/letter/v/f1d935/32.png) [@vic22](https://discuss.elastic.co/u/vic22)
#### Post date: [July 10, 2020, 9:58am UTC](https://discuss.elastic.co/t/elasticsearch-term-aggregations-use-case-for-a-single-field/240672/1 "2020-07-10T09:58:52Z")

</div>

Hello everyone,

I try to understand term aggregations in Elasticsearch, and I don't know how to fix an particular use case : let me explain !  
I try to catch all the element and their "key" values of a single field within my index.

Here it's my aggs request :  
POST bigginsight/\_search?track\_total\_hits=true  
{  
"aggs": {  
" all\_values\_\_downloadTotal": {  
"terms": {  
"field": "downloadTotal"  
}  
}  
},  
"size": 0  
}

and the result of it :  
"aggregations": {  
" all\_values\_\_downloadTotal": {  
**"doc\_count\_error\_upper\_bound": 77,**  
\*\* "sum\_other\_doc\_count": 206696,\*\*  
"buckets": [  
{  
"key": 0,  
"doc\_count": 35920  
},  
{  
"key": 1791,  
"doc\_count": 35  
},  
{  
...  
...

And, i would like to know how to avoid this row to happen :"doc\_count\_error\_upper\_bound": 77,  
\*\* "sum\_other\_doc\_count": 206696,  
Because I want all the elements and their value in this field, such as an aray with "key" values and the number of elements which has it.  
Maybe I'm using the wrong kind of request ?

If somebody can help me, I will be glad to hear from him,

Thanks in advance ! And good day !

---

<div class="post-metadata">

### Author: ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)
#### Post date: [July 13, 2020, 4:13am UTC](https://discuss.elastic.co/t/elasticsearch-term-aggregations-use-case-for-a-single-field/240672/2 "2020-07-13T04:13:12Z")

</div>

@vic22  
Since you haven't specified size for terms aggregation it will default to 10. Results indicate that your index has 206,696 (sum\_other\_doc\_count) documents that are not in these 10 buckets (0, 1791 etc.) As you increase size this number will drop. But query will be more expensive. If you are happy with top 10 results, you don't need to worry about this number.

Doc counts are approximate. doc\_count\_error\_upper\_bound = 77 means, doc\_count for any bucket **may be off maximum** by 77. There is a shard\_size parameter for the terms aggregation. If you increase value you will be able to reduce error upper bound. But query will be more expensive. For high cardinality field making it 0 will be cost prohibitive.

If you really need counts to be 100% accurate **for the top 10** , you can first get top 10 then run same query again with additional filter on downloadTotal field to include only top 10 values (0, 1791 etc).

see [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#\_calculating\_document\_count\_error](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_calculating_document_count_error)

---

<div class="post-metadata">

### Author: ![vic22](https://avatars.discourse-cdn.com/v4/letter/v/f1d935/32.png) [@vic22](https://discuss.elastic.co/u/vic22)
#### Post date: [July 15, 2020, 12:40pm UTC](https://discuss.elastic.co/t/elasticsearch-term-aggregations-use-case-for-a-single-field/240672/3 "2020-07-15T12:40:02Z")

</div>

Thanks you very much, I truly understand my mistake.  
Have a good end of week !

---

<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: [August 12, 2020, 12:40pm UTC](https://discuss.elastic.co/t/elasticsearch-term-aggregations-use-case-for-a-single-field/240672/4 "2020-08-12T12:40:08Z")

</div>

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