# Aggs returning more Cardinality than Source documents

**URL:** https://discuss.elastic.co/t/aggs-returning-more-cardinality-than-source-documents/268142
**Category:** Elasticsearch
**Created:** [March 23, 2021, 9:44pm UTC](https://discuss.elastic.co/t/aggs-returning-more-cardinality-than-source-documents/268142 "2021-03-23T21:44:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LeeBear](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@LeeBear](https://discuss.elastic.co/u/LeeBear)
#### Post date: [March 23, 2021, 9:44pm UTC](https://discuss.elastic.co/t/aggs-returning-more-cardinality-than-source-documents/268142/1 "2021-03-23T21:44:20Z")

</div>

I am running:

```auto
{
	"size": 0,
	"track_total_hits": true,
	"query": {
		"bool": {
			"must": [
				{"match": {"prgx_email_join": {"query": "attachmentChild" } } }
			] 
		}
	},
	"aggs": {
		"attachment_count" : {
			"cardinality": {
				"script": {
					"source": "doc['_id'].value.substring(48)"
				}
			}
		}
        }
}

```

And the results are:

```auto
{
  "took": 20433,
  "timed_out": false,
  "_shards": {
    "total": 6,
    "successful": 6,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 34924205,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "attachment_count": {
      "value": 35113251
    }
  }
}

```

How does the **attachment\_count** exceed the **hits.total.value**?

---

<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: [March 23, 2021, 10:39pm UTC](https://discuss.elastic.co/t/aggs-returning-more-cardinality-than-source-documents/268142/2 "2021-03-23T22:39:48Z")

</div>

Hi Lee,  
From [the docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html):

> A `single-value` metrics aggregation that calculates an approximate count of distinct values.

The key word here is approximate - exact counts are not possible at scale with speed.

---

<div class="post-metadata">

### Author: ![LeeBear](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@LeeBear](https://discuss.elastic.co/u/LeeBear)
#### Post date: [March 24, 2021, 6:22pm UTC](https://discuss.elastic.co/t/aggs-returning-more-cardinality-than-source-documents/268142/3 "2021-03-24T18:22:23Z")

</div>

Good to know, I would think that even an approximation would use the upper limit of the match.

---

<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: [March 25, 2021, 6:57pm UTC](https://discuss.elastic.co/t/aggs-returning-more-cardinality-than-source-documents/268142/4 "2021-03-25T18:57:08Z")

</div>

> [@LeeBear](#):
>
> , I would think that even an approximation would use the upper limit of the match

When you have multi-value fields (or scripts that can generate multiple values) there is no upper limit

---

<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: [April 22, 2021, 6:57pm UTC](https://discuss.elastic.co/t/aggs-returning-more-cardinality-than-source-documents/268142/5 "2021-04-22T18:57:19Z")

</div>

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