# Combining aggregations

**URL:** https://discuss.elastic.co/t/combining-aggregations/47816
**Category:** Elasticsearch
**Created:** [April 19, 2016, 5:01pm UTC](https://discuss.elastic.co/t/combining-aggregations/47816 "2016-04-19T17:01:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![stephan2](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@stephan2](https://discuss.elastic.co/u/stephan2)
#### Post date: [April 19, 2016, 5:01pm UTC](https://discuss.elastic.co/t/combining-aggregations/47816/1 "2016-04-19T17:01:41Z")

</div>

Hi community,

I try to combine query aggregations. For example, if I have one main query (title:bike) and 3 aggregation filters (brand:giant, color:red and size:M), I want to get all the possible combinations:  
number of bike that are giant and red and M  
number of bike that are red and not giant,and not M  
and so on...  
I tried to generate a query with all the combinations, but it is quite a big query and I am not sure it is optimal from a performance point of view. Do you think there is a better way to do that with ES?

Thank you,

FYI, the generated query is :

"query" : {  
"query\_string" : {  
"query" : "title:bike"  
}  
},  
"aggregations" : {  
"0" : {  
"filter" : {  
"bool" : {  
"must\_not" : [ {  
"query\_string" : {  
"query" : "color:red"  
}  
}, {  
"query\_string" : {  
"query" : "size:M"  
}  
}, {  
"query\_string" : {  
"query" : "brand:giant"  
}  
} ]  
}  
}  
},  
"1" : {  
"filter" : {  
"bool" : {  
"must" : {  
"query\_string" : {  
"query" : "color:red"  
}  
},  
"must\_not" : [ {  
"query\_string" : {  
"query" : "size:M"  
}  
}, {  
"query\_string" : {  
"query" : "brand:giant"  
}  
} ]  
}  
}  
},  
"2" : {  
"filter" : {  
"bool" : {  
"must" : {  
"query\_string" : {  
"query" : "size:M"  
}  
},  
"must\_not" : [ {  
"query\_string" : {  
"query" : "color:red"  
}  
}, {  
"query\_string" : {  
"query" : "brand:giant"  
}  
} ]  
}  
}  
},  
"3" : {  
"filter" : {  
"bool" : {  
"must" : [ {  
"query\_string" : {  
"query" : "color:red"  
}  
}, {  
"query\_string" : {  
"query" : "size:M"  
}  
} ],  
"must\_not" : {  
"query\_string" : {  
"query" : "brand:giant"  
}  
}  
}  
}  
},  
"4" : {  
"filter" : {  
"bool" : {  
"must" : {  
"query\_string" : {  
"query" : "brand:giant"  
}  
},  
"must\_not" : [ {  
"query\_string" : {  
"query" : "color:red"  
}  
}, {  
"query\_string" : {  
"query" : "size:M"  
}  
} ]  
}  
}  
},  
"5" : {  
"filter" : {  
"bool" : {  
"must" : [ {  
"query\_string" : {  
"query" : "color:red"  
}  
}, {  
"query\_string" : {  
"query" : "brand:giant"  
}  
} ],  
"must\_not" : {  
"query\_string" : {  
"query" : "size:M"  
}  
}  
}  
}  
},  
"6" : {  
"filter" : {  
"bool" : {  
"must" : [ {  
"query\_string" : {  
"query" : "size:M"  
}  
}, {  
"query\_string" : {  
"query" : "brand:giant"  
}  
} ],  
"must\_not" : {  
"query\_string" : {  
"query" : "color:red"  
}  
}  
}  
}  
},  
"7" : {  
"filter" : {  
"bool" : {  
"must" : [ {  
"query\_string" : {  
"query" : "color:red"  
}  
}, {  
"query\_string" : {  
"query" : "size:M"  
}  
}, {  
"query\_string" : {  
"query" : "brand:giant"  
}  
} ]  
}  
}  
}  
}  
}

---

<div class="post-metadata">

### Author: ![stephan2](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@stephan2](https://discuss.elastic.co/u/stephan2)
#### Post date: [April 21, 2016, 7:20am UTC](https://discuss.elastic.co/t/combining-aggregations/47816/2 "2016-04-21T07:20:46Z")

</div>

Hi all,

The solution does't 'look' very optimal. But after some test, it seems that it is not too bad. I think Elasticsearch uses the cache quite well and avoid recalculating the filter each time it need it.  
But another solution (less verbose) would be welcomed if someone has a better idea 🙂

Thanks,

---

<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: [April 21, 2016, 8:34am UTC](https://discuss.elastic.co/t/combining-aggregations/47816/3 "2016-04-21T08:34:51Z")

</div>

See [https://github.com/elastic/elasticsearch/pull/7020#issuecomment-53087546](https://github.com/elastic/elasticsearch/pull/7020#issuecomment-53087546)

Cheers  
Mark

---

<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: [July 5, 2017, 10:57pm UTC](https://discuss.elastic.co/t/combining-aggregations/47816/4 "2017-07-05T22:57:25Z")

</div>


