# How to use multiple Composite Aggregations in ElasticSearch?

**URL:** <https://discuss.elastic.co/t/how-to-use-multiple-composite-aggregations-in-elasticsearch/155193>\
**Category:** Elasticsearch\
**Created:** [November 2, 2018, 3:47pm UTC](https://discuss.elastic.co/t/how-to-use-multiple-composite-aggregations-in-elasticsearch/155193 "2018-11-02T15:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjgarza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kjgarza/32/35416_2.png) [@kjgarza](https://discuss.elastic.co/u/kjgarza)\
**Post date:** [November 2, 2018, 3:47pm UTC](https://discuss.elastic.co/t/how-to-use-multiple-composite-aggregations-in-elasticsearch/155193/1 "2018-11-02T15:47:46Z")

</div>

I am trying to obtain two [composite aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html) in ElasticSearch but the second one is always giving me an empty bucket.

`GET /resolutions/_search`

```
{
	"query": {
		"query_string": {
			"query": "*"
		}
	},
	"aggs": {
		"total": {
			"composite": {
				"sources": [
					{"doi": {"terms": {"field": "doi"}}},
					{"access_method": {"terms": {"field": "access_method"}}}
				],
				"size": 10000
			}
		},
		"unqiue": {
			"composite": {
				"sources": [
					{"doi": {"terms": {"field": "doi"}}},
					{"access_method": {"terms": {"field": "access_method"}}},
					{"session": {"terms": {"field": "session"}}}
				],
				"size": 10000
			}
		}
	},
	"size": 0,
	"track_total_hits": false
}

```

In the response, you can see the first aggregation (`total`) with 1000s of objects in the bucket but the second one aggreagtion (`unique`) is always empty. I have tried swaping the order of the aggregations and it's always the second one in order that is empty.

[![Reponse with second bucket empty][2]][2]

[![enter image description here](https://i.stack.imgur.com/nI40w.png)](https://i.stack.imgur.com/nI40w.png)

The index mapping are in: [https://github.com/datacite/shiba-inu/blob/2d632d341a22a8dca2afec3b01c3b34030144c9c/templates/aggregating\_es.json](https://github.com/datacite/shiba-inu/blob/2d632d341a22a8dca2afec3b01c3b34030144c9c/templates/aggregating_es.json)

Why is it returning an empty bucket?

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [November 12, 2018, 3:53pm UTC](https://discuss.elastic.co/t/how-to-use-multiple-composite-aggregations-in-elasticsearch/155193/2 "2018-11-12T15:53:57Z")

</div>

> Why is it returning an empty bucket?

This is a bug ☹  
The execution of the first composite aggregation breaks the second one. The [fix](https://github.com/elastic/elasticsearch/pull/33279) is already committed and will be available soon (in `6.5.0`).

---

<div class="post-metadata">

**Author:** ![kjgarza](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kjgarza/32/35416_2.png) [@kjgarza](https://discuss.elastic.co/u/kjgarza)\
**Post date:** [November 12, 2018, 8:02pm UTC](https://discuss.elastic.co/t/how-to-use-multiple-composite-aggregations-in-elasticsearch/155193/3 "2018-11-12T20:02:45Z")

</div>

Thanks

---

<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:** [December 10, 2018, 8:02pm UTC](https://discuss.elastic.co/t/how-to-use-multiple-composite-aggregations-in-elasticsearch/155193/4 "2018-12-10T20:02:46Z")

</div>

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