# Faster way to count maximum cardinality?

**URL:** <https://discuss.elastic.co/t/faster-way-to-count-maximum-cardinality/153199>\
**Category:** Elasticsearch\
**Created:** [October 19, 2018, 4:27pm UTC](https://discuss.elastic.co/t/faster-way-to-count-maximum-cardinality/153199 "2018-10-19T16:27:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![klahnakoski](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klahnakoski/32/3679_2.png) [@klahnakoski](https://discuss.elastic.co/u/klahnakoski)\
**Post date:** [October 19, 2018, 4:27pm UTC](https://discuss.elastic.co/t/faster-way-to-count-maximum-cardinality/153199/1 "2018-10-19T16:27:26Z")

</div>

I am collecting metadata on properties:

```
{
	"aggs":{
		"card":{"cardinality":{"field":"task.dependencies"}},
		"multi":{"max":{"script":"doc[\"task.dependencies\"].values.size()"}}
	},
	"size":0
}

```

The first is cardinality, which helps decide if using terms aggregation is a reasonable thing to do. The second is "multi" which counts the number of terms a single document property can have. These "multi-valued" properties must be treated different from single-values properties when aggregating.

`{"script":"doc[\"task.dependencies\"].values.size()"}` is slow. Is there a faster way to detect if a property has been given more-than-one value?

Thank you

---

<div class="post-metadata">

**Author:** ![klahnakoski](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klahnakoski/32/3679_2.png) [@klahnakoski](https://discuss.elastic.co/u/klahnakoski)\
**Post date:** [October 20, 2018, 11:41am UTC](https://discuss.elastic.co/t/faster-way-to-count-maximum-cardinality/153199/2 "2018-10-20T11:41:59Z")

</div>

Since it is uncommon for single-valued properties to suddenly have multiple values, and multi-valued properties are commonly multi-valued (like `text` type), then I need not scan all the data; performing this aggregation on only a recent subset. I make a new index for each week of data. I chose a property that limits my query to recent data. I assume the `script` is only executed on a single index (or two), rather than all indexes. This reduces the `script` workload, and the query runs faster.

```
{
    "aggs":{
        "count":{"cardinality":{"field":"task.dependencies"}},
        "_filter":{
            "aggs":{"multi":{"max":{"script":"doc[\"task.dependencies\"].values.size()"}}},
            "filter":{"bool":{"should":[
                {"term":{"etl.timestamp.~n~":1539388800}},
                {"missing":{"field":"etl.timestamp.~n~"}}
            ]}}
        }
    },
    "size":0
}
```

---

<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:** [November 17, 2018, 11:42am UTC](https://discuss.elastic.co/t/faster-way-to-count-maximum-cardinality/153199/3 "2018-11-17T11:42:00Z")

</div>

Closed on November 17, 2018, 11:42am UTC

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