# Aggregation - Calculate the number of distinct values

**URL:** <https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499>\
**Category:** Elasticsearch\
**Created:** [December 5, 2018, 9:36am UTC](https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499 "2018-12-05T09:36:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandra1](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Alexandra1](https://discuss.elastic.co/u/Alexandra1)\
**Post date:** [December 5, 2018, 9:36am UTC](https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499/1 "2018-12-05T09:36:19Z")

</div>

Hello,

I'm doing an aggregation over all the documents of my index. The problem is that Elasticsearch doesn't return all the buckets because of the default size. How i can find out the number of all the distinct buckets key in order to know which size to put on the aggregation.

This is the query:

{"size": 0,  
"aggs" : {  
"genres" : {  
"nested" : {  
"path" : "tags"  
},  
"aggs": {  
"key\_value":{  
"terms" : {  
"field" : "tags.values.value"  
}  
}  
}}}}

and the result looks like:

},  
"aggregations": {  
"genres": {  
"doc\_count": 285391,  
"key\_value": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 46823,  
"buckets": [  
{  
"key": "null",  
"doc\_count": 251811  
},  
{  
"key": "mmakku",  
"doc\_count": 15870  
},  
{  
"key": "mmbaden",  
"doc\_count": 15870  
},  
{  
"key": "mmettlingen",  
"doc\_count": 15870  
},  
{  
"key": "mmeurope",  
"doc\_count": 15870  
},  
{  
"key": "mmgermany",  
"doc\_count": 15870  
},  
{  
"key": "mmkarlsruhe",  
"doc\_count": 15870  
}

Thank you!

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [December 5, 2018, 9:39am UTC](https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499/2 "2018-12-05T09:39:12Z")

</div>

@Alexandra1 I would recommend you to look at cardinality aggregations. Documentation for that can be found [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html).

---

<div class="post-metadata">

**Author:** ![balumurari1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/balumurari1/32/39203_2.png) [@balumurari1](https://discuss.elastic.co/u/balumurari1)\
**Post date:** [December 5, 2018, 10:40am UTC](https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499/3 "2018-12-05T10:40:24Z")

</div>

Here is the sample, suppose to get distinct members based on tag\_id  
GET index\_name/\_search  
{  
"size" : 0,  
"aggs" : {  
"distinct\_members" : {  
"cardinality" : {  
"field" : "tag\_id.keyword"  
}  
}  
}

}

---

<div class="post-metadata">

**Author:** ![Alexandra1](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Alexandra1](https://discuss.elastic.co/u/Alexandra1)\
**Post date:** [December 6, 2018, 8:14am UTC](https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499/4 "2018-12-06T08:14:55Z")

</div>

Seems to work with cardinality. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![balumurari1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/balumurari1/32/39203_2.png) [@balumurari1](https://discuss.elastic.co/u/balumurari1)\
**Post date:** [December 6, 2018, 8:22am UTC](https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499/5 "2018-12-06T08:22:23Z")

</div>

cheers!! mark it as a solution

---

<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:** [January 3, 2019, 8:22am UTC](https://discuss.elastic.co/t/aggregation-calculate-the-number-of-distinct-values/159499/6 "2019-01-03T08:22:35Z")

</div>

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