# Can I calculate the cardinality of the \_id field?

**URL:** <https://discuss.elastic.co/t/can-i-calculate-the-cardinality-of-the--id-field/155>\
**Category:** Elasticsearch\
**Created:** [May 4, 2015, 9:39pm UTC](https://discuss.elastic.co/t/can-i-calculate-the-cardinality-of-the--id-field/155 "2015-05-04T21:39:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dave](https://discuss.elastic.co/u/dave)\
**Post date:** [May 4, 2015, 9:39pm UTC](https://discuss.elastic.co/t/can-i-calculate-the-cardinality-of-the--id-field/155/1 "2015-05-04T21:39:55Z")

</div>

I have multiple Elasticsearch 1.3.2 indices, and I'm using custom  
document IDs. I want to find the number of distinct IDs across my  
indices. Some documents have the same ID but are in different indices,  
so this is different from just counting documents. So I wanted to do a  
cardinality aggregation on the \_id field. So I posted this to [http://localhost:9200/](http://localhost:9200/)_my\_indices_/\_search:

{ "from": 0, "size": 0, "aggregations": { "\_count": { "cardinality": { "script": "doc['\_id'].value", "lang": "groovy" } } } }

But Elasticsearch just sent back this:

{ "took": 60, "timed\_out": false, "\_shards": { "total": 175, "successful": 175, "failed": 0 }, "hits": { "total": 310714, "max\_score": 0, "hits": [] }, "aggregations": { "\_count": { "value": 0 } }

I'm pretty sure there's more than 0 IDs in there! What happened, and is it possible to get what I want?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 6, 2015, 12:34pm UTC](https://discuss.elastic.co/t/can-i-calculate-the-cardinality-of-the--id-field/155/2 "2015-05-06T12:34:09Z")

</div>

The issue here is that the \_id field is neither indexed nor has doc values so aggregations can't load fielddata. You can try the \_uid field instead (which is a combination of the \_type and the \_id) which is indexed. Note however that this will require a lot of memory because of fielddata.

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [May 6, 2015, 1:01pm UTC](https://discuss.elastic.co/t/can-i-calculate-the-cardinality-of-the--id-field/155/3 "2015-05-06T13:01:28Z")

</div>

i think that is really important to say that this result is approximate, in a small index i had good results after one/two million of docs the result is not accurate almost every time (after as the doc says the error remains under 5%)

---

<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 6, 2017, 12:15am UTC](https://discuss.elastic.co/t/can-i-calculate-the-cardinality-of-the--id-field/155/4 "2017-07-06T00:15:23Z")

</div>


