# Optimize multiple queries with the same filters

**URL:** <https://discuss.elastic.co/t/optimize-multiple-queries-with-the-same-filters/236925>\
**Category:** Elasticsearch\
**Created:** [June 12, 2020, 5:01pm UTC](https://discuss.elastic.co/t/optimize-multiple-queries-with-the-same-filters/236925 "2020-06-12T17:01:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![solenom85](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/solenom85/32/68154_2.png) [@solenom85](https://discuss.elastic.co/u/solenom85)\
**Post date:** [June 12, 2020, 5:01pm UTC](https://discuss.elastic.co/t/optimize-multiple-queries-with-the-same-filters/236925/1 "2020-06-12T17:01:25Z")

</div>

Hi I have a question related with optimization, I have a page lets call it **global page** that haves **N** components on each.

Each component will perform an elasticsearch query that is going to be composed of two parts

1. Query is set at global page level
2. Aggregation is created by the component.

The current way of things make each component to generate a full query using the **global page** generated query and the component's aggregation. Which results in the global page making \>= **N** requests all of them using the same query.

The requests have size=0 encouraging caching of the aggregated results and that didn't resulted since the cache key is the full query json and it won't work when it changes. I wonder if there is a way to improve this.

I was looking solutions like locking results but so far I have found none.

I also tried running a prep query fetching the document ids and then aggregating on each component over a query matching the ids with the specific aggregation but since we have too many documents for some cases the payload can get pretty big since the ids are long strings.

Any advice?

---

<div class="post-metadata">

**Author:** ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)\
**Post date:** [June 14, 2020, 3:28pm UTC](https://discuss.elastic.co/t/optimize-multiple-queries-with-the-same-filters/236925/2 "2020-06-14T15:28:35Z")

</div>

Hi Solenom85,  
Instead of running query from each component I would try to collect component aggregations at the global page level and run a single query with all aggregates.

As for caching, if you have a primary and a replica your first query may go to primary and second may go to replica.

---

<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 12, 2020, 3:28pm UTC](https://discuss.elastic.co/t/optimize-multiple-queries-with-the-same-filters/236925/3 "2020-07-12T15:28:46Z")

</div>

Closed on July 12, 2020, 3:28pm UTC

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