# Combining aggregations in a query

**URL:** <https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676>\
**Category:** Elasticsearch\
**Created:** [July 22, 2019, 4:15pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676 "2019-07-22T16:15:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JamesPeck](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JamesPeck](https://discuss.elastic.co/u/JamesPeck)\
**Post date:** [July 22, 2019, 4:15pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676/1 "2019-07-22T16:15:13Z")

</div>

I am new to Elasticsearch queries and I am trying to combine two aggregations into one aggregation field.

Below I have the basic idea I am trying to get to. I realize that statusCategoriesCombo wont work that way, but I think it helps demonstrate what I am after.

```
"aggs": {
	"first_specialties": {
		"terms": {
			"field": "firstToDesc"
		}
	},
	"second_specialties": {
		"terms": {
			"field": "secondToDesc"
		}
	},
	"statusCategories": {
		"statusCategoriesCombo": { first_specialties + second_specialties}
	}
}

```

Is it possible to do something like this? If so, how? And if not do you have any suggestions to solve my problem?

I should also note that I am querying multiple indexes at once. first\_specialties and second\_specialties are in different indexes.

We are using ES 6.3.2

---

<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:** [July 22, 2019, 4:22pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676/2 "2019-07-22T16:22:04Z")

</div>

Hi James,

> [@JamesPeck](#):
>
> first\_specialties and second\_specialties are in different indexes.

I'm not sure what these aggregations would represent. Ordinarily aggregations are used to account for docs that bring certain combinations of things together eg `department` and `product`. For that to happen you have to have a doc that has these two fields in order to make a relationship.

If you have 2 different fields in 2 different indices it's hard to guess what logic might associate pairs of these values together?

---

<div class="post-metadata">

**Author:** ![JamesPeck](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JamesPeck](https://discuss.elastic.co/u/JamesPeck)\
**Post date:** [July 22, 2019, 4:26pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676/3 "2019-07-22T16:26:31Z")

</div>

They output something like this:

```
   "first_specialties": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "FACILITY / SERVICE",
          "doc_count": 4
        }

```

"second\_specialties" would have the exact same thing with a different doc\_count

---

<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:** [July 22, 2019, 4:33pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676/4 "2019-07-22T16:33:37Z")

</div>

Right. But no single doc would have a `first_specialities` value AND a `second_specialities` value so the intersection of values in these sets is zero - no results.

---

<div class="post-metadata">

**Author:** ![JamesPeck](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@JamesPeck](https://discuss.elastic.co/u/JamesPeck)\
**Post date:** [July 22, 2019, 4:35pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676/5 "2019-07-22T16:35:30Z")

</div>

Right so what I am really asking is how can I combine the count if the keys match into one agg field. Do I need to use a script for that?

---

<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:** [July 22, 2019, 4:40pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676/6 "2019-07-22T16:40:57Z")

</div>

If these 2 fields existed in a doc, yes, you could use a script to count the number of times values co-occurred.  
Because these 2 fields exist in different indices (and therefore different docs) your script would just generate "pairs" that were of the form: `first_speciality:foo + second_speciality:null` and `first_speciality:null + second_speciality:bar`

---

<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:** [August 19, 2019, 4:40pm UTC](https://discuss.elastic.co/t/combining-aggregations-in-a-query/191676/7 "2019-08-19T16:40:58Z")

</div>

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