# Is the precision of cardinality aggregation decided by total unique value count or filtered unique value count?

**URL:** <https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073>\
**Category:** Elasticsearch\
**Created:** [December 11, 2023, 4:40pm UTC](https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073 "2023-12-11T16:40:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrhoi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henrhoi/32/122956_2.png) [@henrhoi](https://discuss.elastic.co/u/henrhoi)\
**Post date:** [December 11, 2023, 4:40pm UTC](https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073/1 "2023-12-11T16:40:21Z")

</div>

Hi,

We have an index with a field containing ~30,000 unique values.  
When doing a filtered cardinality aggregation on this field, which should return ~650 unique values, we experience non-deterministic results (±25).

We are using a precision threshold of 10,000. With this configuration counts are expected to be close to accurate under 10,000 unique values, or does this limit apply to the total value count?

Thank you in advance!  
Best,  
Henrik

---

<div class="post-metadata">

**Author:** ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)\
**Post date:** [December 12, 2023, 12:42am UTC](https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073/2 "2023-12-12T00:42:20Z")

</div>

Hi @henrhoi,

Two questions;

1. Could you provide which version of Elasticsearch you're using?
2. Could you provide an example query which you are running?

As noted [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_counts_are_approximate) there is no "guarantee" of accuracy, and in the bullet point under [precision control](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_precision_control) it subtly mentions:

> The `precision_threshold` options allows to trade memory for accuracy, and defines a unique count below which counts are _ **expected to be close to accurate** _

Though seeing your query might provide more insight into potential optimizations. You can also look at, [Accurate Distinct Count and Values from Elasticsearch. | by Pratik Patil | Medium](https://pratik-patil.medium.com/accurate-distinct-count-and-values-from-elasticsearch-dadce51ad4a7), which provides some good examples of getting "accurate" counts at the cost of speed.

---

<div class="post-metadata">

**Author:** ![henrhoi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henrhoi/32/122956_2.png) [@henrhoi](https://discuss.elastic.co/u/henrhoi)\
**Post date:** [December 12, 2023, 9:55am UTC](https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073/3 "2023-12-12T09:55:52Z")

</div>

Thank you for the quick response. See my answers below.

1. We are using version 7.16.2.
2. Providing an example query:

For context, the split below is used in a pivot table, without any splits, where the Total-script is used to get this value under a "Total" bucket.

```auto
{
    "aggs": {
        "column_1": {
            "aggs": {
                "value_524933379": {
                    "cardinality": {
                        "field": "some_field",
                        "precision_threshold": 10000
                    }
                }
            },
            "terms": {
                "missing": -1,
                "order": {
                    "_key": "asc"
                },
                "script": {
                    "lang": "painless",
                    "source": "(('Total').toString())"
                },
                "shard_size": 200,
                "size": 10
            }
        }
    },
    "query": {
        "filter": [],
        "must": [
            {
                "terms": {
                    "another_field": [
                        "SOME VALUE"
                    ]
                }
            }
        ]
    },
    "size": 0,
    "track_total_hits": false
}

```

Thank you.

---

<div class="post-metadata">

**Author:** ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)\
**Post date:** [December 12, 2023, 11:48am UTC](https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073/4 "2023-12-12T11:48:23Z")

</div>

Overall, the query looks relatively good, two possible suggestions (I'm not 100% confident they'll improve results though).

1. For your `query`, try using `filter` instead of `must` if you don't need the scoring that `must` offers.
2. Try using the [`execution_hint`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_execution_hint_4) `direct` (or play around with the other possible options as well) to see if you get better accuracy.

---

<div class="post-metadata">

**Author:** ![henrhoi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henrhoi/32/122956_2.png) [@henrhoi](https://discuss.elastic.co/u/henrhoi)\
**Post date:** [December 13, 2023, 1:07pm UTC](https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073/5 "2023-12-13T13:07:14Z")

</div>

Thanks, Ben!

I'll try those suggestions.

In general, what behaviour or accuracy should we expect when aggregating on full vs filtered data? Should we expect accurate results when the data is filtered to \<1000 unique values?

Best,  
Henrik

---

<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:** [January 10, 2024, 1:08pm UTC](https://discuss.elastic.co/t/is-the-precision-of-cardinality-aggregation-decided-by-total-unique-value-count-or-filtered-unique-value-count/349073/6 "2024-01-10T13:08:04Z")

</div>

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