# Cardinality is more than Count. How to achieve the exact uniq count?

**URL:** <https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165>\
**Category:** Elasticsearch\
**Created:** [October 17, 2016, 8:05am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165 "2016-10-17T08:05:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaliph](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@jaliph](https://discuss.elastic.co/u/jaliph)\
**Post date:** [October 17, 2016, 8:05am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/1 "2016-10-17T08:05:11Z")

</div>

**Elasticsearch version** : 2.4.0  
Plugins installed: []  
JVM version: 1.8.0\_101  
**RAM** : 16331784kb  
**Elasticsearch Heap Size** :7g

OS version:Red Hat Enterprise Linux Server release 6.6 (Santiago)

Description of the problem including expected versus actual behavior:  
We have data set that we have loaded in Elasticsearch.

10 Shards, No replica. Index.compression is set to Best.  
Elasticsearch:Port/index\*/\_count  
Count: 527670245

Now this is exact.

We are trying to get the exact unique count of ids in this data set.  
{  
"size" : 0,  
"aggs" : {  
"ids" : {  
"cardinality" : {  
"field" : "id"  
}  
}  
}  
}  
Cardinality Count is : **531551491**  
This is exceeding the total count. This value is absurd and cant be accepted.

On increasing the precision\_threshold to 22000  
Cardinality Count is : **526981997**  
{  
"size" : 0,  
"aggs" : {  
"ids" : {  
"cardinality" : {  
"field" : "id",  
"precision\_threshold" : 22000  
}  
}  
}  
}

This is less than the actual unique id, which actually same as equal to the count. In other words, we have made sure all ids are unique and the cardinality should match the count.

Is there any way to get the cardinality absolute exact value? In what way we can achieve this? Is this purely approximate? Can with increase in Memory we can achieve this?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 17, 2016, 8:09am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/2 "2016-10-17T08:09:38Z")

</div>

Have a read of [https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-aggregations-metrics-cardinality-aggregation.html#\_counts\_are\_approximate](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-aggregations-metrics-cardinality-aggregation.html#_counts_are_approximate)

---

<div class="post-metadata">

**Author:** ![jaliph](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@jaliph](https://discuss.elastic.co/u/jaliph)\
**Post date:** [October 17, 2016, 8:12am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/3 "2016-10-17T08:12:19Z")

</div>

I have already gone through all the documents. Can you suggest some ways to get the unique exact values? The use-case we are using this is a revenue generating model, can't have approx. values to be precise.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 17, 2016, 8:13am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/4 "2016-10-17T08:13:03Z")

</div>

You need to increase the `precision_threshold`, that's the only way.

---

<div class="post-metadata">

**Author:** ![jaliph](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@jaliph](https://discuss.elastic.co/u/jaliph)\
**Post date:** [October 17, 2016, 8:15am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/5 "2016-10-17T08:15:10Z")

</div>

Tried that.. But the values coming are still not exact. The values differ from 22000 to 44000 and reduces to a lower value. Please go through my observation. At an expense of time, I'm ready but i need to get the exact unique values.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 17, 2016, 8:18am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/6 "2016-10-17T08:18:58Z")

</div>

You cannot get it via cardinality.  
Maybe a scan/scroll with some client side code would do it.

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [October 17, 2016, 8:52am UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/7 "2016-10-17T08:52:43Z")

</div>

> [@jaliph](#):
>
> Is there any way to get the cardinality absolute exact value?

You would have to write a plugin for true cardinality aggregation.

---

<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:** [July 5, 2017, 10:11pm UTC](https://discuss.elastic.co/t/cardinality-is-more-than-count-how-to-achieve-the-exact-uniq-count/63165/8 "2017-07-05T22:11:47Z")

</div>


