# Query to get exact cardinality counts

**URL:** <https://discuss.elastic.co/t/query-to-get-exact-cardinality-counts/180836>\
**Category:** Elasticsearch\
**Created:** [May 13, 2019, 2:57pm UTC](https://discuss.elastic.co/t/query-to-get-exact-cardinality-counts/180836 "2019-05-13T14:57:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lask001](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lask001](https://discuss.elastic.co/u/lask001)\
**Post date:** [May 13, 2019, 2:57pm UTC](https://discuss.elastic.co/t/query-to-get-exact-cardinality-counts/180836/1 "2019-05-13T14:57:29Z")

</div>

I'm trying to get an exact count of documents that meet a specific criteria. Aggs queries are somewhat helpful, but I need to be able to return results as well so hoping someone knows a good way to approach this problem. Here are what some of my documents look like:

```
{
  "doc_id": 1,
  "doc_type": "foo"
}

{
  "doc_id": 1,
  "doc_type": "foo"
}

{
  "doc_id": 2,
  "doc_type": "foo"
}

{
  "doc_id": 2,
  "doc_type": "bar"
}

```

The criteria I'm searching for is documents that have the same `doc_id` but more than one unique value for `doc_type`. In the above example `doc_id = 1` would be fine and not picked up by my query, but `doc_id = 2` is bad and I need to capture both the `doc_id` and that it's 1 instance of a result meeting my criteria. Does anyone know a good method to generate this information quickly? Currently I've got some python code that generates a list of every `doc_id` and then searches on them all individually and gets the unique values... but that's not very quick and I have millions of documents. Is there a better way to go about this? I know a cardinality query would work but my understanding is the counts aren't exact and as this spans over multiple shards I'm not sure I can count on those results. Hoping there is a more efficient way than I'm currently approaching the problem to solve this.

---

<div class="post-metadata">

**Author:** ![YvorL](https://avatars.discourse-cdn.com/v4/letter/y/9fc348/32.png) [@YvorL](https://discuss.elastic.co/u/YvorL)\
**Post date:** [May 14, 2019, 2:24pm UTC](https://discuss.elastic.co/t/query-to-get-exact-cardinality-counts/180836/2 "2019-05-14T14:24:45Z")

</div>

Hi!

In case, you absolutely need 100% accuracy for the count, you might not be able to use cardinality ([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html)), but my experience shows that it's really close to the value I'm looking for. Of course it depends on shard and document number. In case you only need to know if there are more than one "types", I'd say (though it's a guess) that you will get that info, just not the correct count. Maybe `precision_threshold` can help you out.  
Otherwise, you can try simply getting the `doc_type` count by using a terms aggregation and check if there are more than one buckets.

---

<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:** [June 11, 2019, 2:24pm UTC](https://discuss.elastic.co/t/query-to-get-exact-cardinality-counts/180836/3 "2019-06-11T14:24:45Z")

</div>

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