# How to speed up aggregated query?

**URL:** <https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190>\
**Category:** Elasticsearch\
**Created:** [December 17, 2019, 3:58pm UTC](https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190 "2019-12-17T15:58:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![111230](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111230/32/47196_2.png) [@111230](https://discuss.elastic.co/u/111230)\
**Post date:** [December 17, 2019, 3:58pm UTC](https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190/1 "2019-12-17T15:58:56Z")

</div>

I have the following query:  
`{'size': 0, 'aggs': {'count_group': {'filters': {'filters': {'89634469': {'term': {'supplier.id': 89634469}}, '89633422': {'term': {'supplier.id': 89633422}}}}, 'aggs': {'market': {'terms': {'field': 'supplier_nom.id', 'size': 100000}}}}}, 'query': {'bool': {'must': [{'has_parent': {'parent_type': 'doc', 'query': {'bool': {'must': [{'range': {'created': {'lte': 'now/d'}}}, {'range': {'expires': {'gte': 'now/d'}}}]}}}}, {'term': {'doc_type': 'item'}}]}}}`  
The result looks like this

 ![Снимок экрана 2019-12-17 в 22.53.52](https://us1.discourse-cdn.com/elastic/original/3X/b/e/be0ec6b98f71f8f51b51f25a47cf7ee3a8240a25.jpeg)

It works well, but if I put a much more supplier.ids, It begin to works much slower. Any suggestions to improve speed?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 17, 2019, 5:03pm UTC](https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190/2 "2019-12-17T17:03:23Z")

</div>

You aggregation is pretty hard to read, so I may be wrong.

as you can see in the `hits.total` field, there are still 280k documents that need to be filtered for.

Can you create an additional filter [terms query](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-terms-query.html) in the query to only filter for the ids that you are interested in? This would reduce the total number of documents, that you have to aggregate against and likely give you a speedup.

---

<div class="post-metadata">

**Author:** ![111230](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111230/32/47196_2.png) [@111230](https://discuss.elastic.co/u/111230)\
**Post date:** [December 19, 2019, 9:40am UTC](https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190/3 "2019-12-19T09:40:06Z")

</div>

Thank you for your answer. Probably, i should clarify.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/f/5fbf48ce70b23d6e74f3f30d3db3347382012e89.png)  
At existing approach, i put ids in filters field and then aggregate result through field supplier\_nom.id. Then i have much more ids, i lose speed. Is there any way to reconstruct query in order to gain speed with the same logic?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 19, 2019, 9:53am UTC](https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190/4 "2019-12-19T09:53:11Z")

</div>

please look at the term query I mentioned above.

---

<div class="post-metadata">

**Author:** ![111230](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111230/32/47196_2.png) [@111230](https://discuss.elastic.co/u/111230)\
**Post date:** [December 19, 2019, 10:52am UTC](https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190/5 "2019-12-19T10:52:38Z")

</div>

How about composite aggregation?

---

<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:** [January 16, 2020, 10:52am UTC](https://discuss.elastic.co/t/how-to-speed-up-aggregated-query/212190/6 "2020-01-16T10:52:44Z")

</div>

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