# I want to get the exact distinct count and the docs in query

**URL:** https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330
**Category:** Elasticsearch
**Created:** [August 25, 2020, 5:04pm UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330 "2020-08-25T17:04:39Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![nandalapadu\_shamriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nandalapadu_shamriya/32/74482_2.png) [@nandalapadu\_shamriya](https://discuss.elastic.co/u/nandalapadu_shamriya)
#### Post date: [August 25, 2020, 5:04pm UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/1 "2020-08-25T17:04:39Z")

</div>

I am new to ES,i have a requirement where i need to get the distinct values and distinct exact count of records till 60000 from elastic search query but my query is always returning the approximate counts only.Can some one help?

---

<div class="post-metadata">

### Author: ![KrishnaShah7](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krishnashah7/32/66810_2.png) [@KrishnaShah7](https://discuss.elastic.co/u/KrishnaShah7)
#### Post date: [August 26, 2020, 4:53am UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/2 "2020-08-26T04:53:21Z")

</div>

Hi @nandalapadu_shamriya, Welcome to the community.  
Could you please elaborate on this some more. Could you tell us what is the dsl query you're using for doing the count?

---

<div class="post-metadata">

### Author: ![nandalapadu\_shamriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nandalapadu_shamriya/32/74482_2.png) [@nandalapadu\_shamriya](https://discuss.elastic.co/u/nandalapadu_shamriya)
#### Post date: [August 26, 2020, 5:29am UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/3 "2020-08-26T05:29:51Z")

</div>

POST /index/\_doc\_search  
{  
"from": 1,  
"size": 1000,  
"timeout": "60s",  
"query": {  
"bool": {  
"filter": [  
{  
"term": {  
"entity\_type\_code": {  
"value": 5,  
"boost": 1  
}  
}  
},  
{  
"term": {  
"country\_id": {  
"value": 1,  
"boost": 1  
}  
}  
},  
{  
"term": {  
"period\_type\_code": {  
"value": 100,  
"boost": 1  
}  
}  
},  
{  
"terms": {  
"platform\_type\_code": [  
0  
],  
"boost": 1  
}  
},  
{  
"bool": {  
"should": [  
{  
"term": {  
"data\_date": {  
"value": "2020-04-13",  
"boost": 1  
}  
}  
}  
],  
"adjust\_pure\_negative": true,  
"boost": 1  
}  
}  
],  
"adjust\_pure\_negative": true,  
"boost": 1  
}  
},  
"\_source": {  
"includes": [  
"brand\_id",  
"brand\_name",  
"sub\_brand\_id",  
"sub\_brand\_name",  
"asset\_id",  
"asset\_name",  
"asset\_segment\_id",  
"asset\_segment\_value",  
"category\_id",  
"category\_name",  
"subcategory\_id",  
"subcategory\_name",  
"entity\_type\_code",  
"entity\_type\_desc"  
],  
"excludes":   
},  
"sort": [  
{  
"asset\_segment\_value.keyword": {  
"order": "asc"  
}  
},  
{  
"asset\_segment\_value.keyword": {  
"order": "asc"  
}  
}  
],  
"aggregations": {  
"asset\_segment\_id": {  
"cardinality": {  
"field": "asset\_segment\_id"  
}  
}  
},  
"collapse": {  
"field": "asset\_segment\_id"  
}  
}

i used cardinality to get the distinct count on a field asset\_segment\_id, but i am not getting the exact count.The number of docs returned by query is not match with count calculated by cardinality.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [August 26, 2020, 6:19am UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/4 "2020-08-26T06:19:05Z")

</div>

Cardinality aggregations in Elasticsearch are always an approximation. Have a look at [this thread](https://discuss.elastic.co/t/is-it-true-that-sometimes-elasticsearch-aggregations-can-be-inaccurate/156396/2) for discussion on the topic.

---

<div class="post-metadata">

### Author: ![nandalapadu\_shamriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nandalapadu_shamriya/32/74482_2.png) [@nandalapadu\_shamriya](https://discuss.elastic.co/u/nandalapadu_shamriya)
#### Post date: [August 26, 2020, 7:10am UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/5 "2020-08-26T07:10:56Z")

</div>

Any other way to get distinct exact count apart from cardinality.?

---

<div class="post-metadata">

### Author: ![nandalapadu\_shamriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nandalapadu_shamriya/32/74482_2.png) [@nandalapadu\_shamriya](https://discuss.elastic.co/u/nandalapadu_shamriya)
#### Post date: [August 28, 2020, 2:42pm UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/6 "2020-08-28T14:42:29Z")

</div>

i read through the elastic documents it was said that up to the precision threshold 40000 the count are accurate. but i still see 2-3 differences between them.please help

---

<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 28, 2020, 5:33pm UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/7 "2020-08-28T17:33:36Z")

</div>

Precision threshold is not the boundary between accurate and inaccurate. It is the threshold between one fuzzy counting technique and another. Below the precision threshold it is a count of hashes (that can have a collision but is relatively rare).

---

<div class="post-metadata">

### Author: ![nandalapadu\_shamriya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nandalapadu_shamriya/32/74482_2.png) [@nandalapadu\_shamriya](https://discuss.elastic.co/u/nandalapadu_shamriya)
#### Post date: [August 28, 2020, 5:49pm UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/8 "2020-08-28T17:49:16Z")

</div>

Thank you for your reply, but I have tried different ways with out collapse field also but all the same returns the result. Without precision the gap is high with precision max 10 difference.can it be depends on the how shards were created and ingestion of data.?

---

<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 25, 2020, 5:49pm UTC](https://discuss.elastic.co/t/i-want-to-get-the-exact-distinct-count-and-the-docs-in-query/246330/9 "2020-09-25T17:49:25Z")

</div>

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