# Why is my composite aggregation faster than a terms bucket aggregation for a high cardinality field?

**URL:** <https://discuss.elastic.co/t/why-is-my-composite-aggregation-faster-than-a-terms-bucket-aggregation-for-a-high-cardinality-field/230324>\
**Category:** Elasticsearch\
**Created:** [April 29, 2020, 7:32am UTC](https://discuss.elastic.co/t/why-is-my-composite-aggregation-faster-than-a-terms-bucket-aggregation-for-a-high-cardinality-field/230324 "2020-04-29T07:32:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideongrossman](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gideongrossman](https://discuss.elastic.co/u/gideongrossman)\
**Post date:** [April 29, 2020, 7:32am UTC](https://discuss.elastic.co/t/why-is-my-composite-aggregation-faster-than-a-terms-bucket-aggregation-for-a-high-cardinality-field/230324/1 "2020-04-29T07:32:24Z")

</div>

I am creating a terms aggregation on a high cardinality field. It takes 4 seconds. That's too long. I notice that if I restructure the query as a composite aggregation, it takes way less time ~400ms. Why might this be? Can the speed improvement be explained by the nature of composite aggregations or is the improvement due to some other feature of my composite aggregation that can be applied to my original terms aggregation?

By the way, my index contains 8 million documents. There are nearly that many unique addresses, i.e. the cardinality of the home\_address1 field is in the millions.

Here is the query that takes 4 seconds...

```
`GET /<indexname>/_search
    {
        "size": "600",
        "timeout": "60s",
        "query": {
            "bool": {
                "must": [
                    {
                        "match_all": {
                            "boost": 1
                        }
                    }
                ],
                "adjust_pure_negative": true,
                "boost": 1
            }
        },
        "track_total_hits": false,
        "aggregations": {
            "home_address1": {
                "terms": {
                    "field": "home_address1.keyword",
                    "size": 10,
                    "order": {
                        "_key": "asc"
                    }
                }
            }
        }
    }`

```

and here is the query that takes 400ms.

```
`GET /<indexname>/_search
    {
    "size": "600",
    "timeout": "60s",
    "query": {
        "bool": {
            "must": [
                {
                    "match_all": {
                        "boost": 1
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "track_total_hits": false,
    "aggregations": {
        "home_address1": {
            "composite": {
                "size": 10,
                "sources": [
                    {
                        "home_address1": {
                            "terms": {
                                "field": "home_address1.keyword",
                                "order": "asc"
                            }
                        }
                    }
                ]
            }
        } 
    }
}`
```

---

<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:** [May 27, 2020, 7:32am UTC](https://discuss.elastic.co/t/why-is-my-composite-aggregation-faster-than-a-terms-bucket-aggregation-for-a-high-cardinality-field/230324/2 "2020-05-27T07:32:28Z")

</div>

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