# \[Question\] Use aggregation result to query documents in a single query to optimise calls to ES

**URL:** <https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775>\
**Category:** Elasticsearch\
**Created:** [May 12, 2021, 7:54am UTC](https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775 "2021-05-12T07:54:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RapTvin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raptvin/32/86995_2.png) [@RapTvin](https://discuss.elastic.co/u/RapTvin)\
**Post date:** [May 12, 2021, 7:54am UTC](https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775/1 "2021-05-12T07:54:21Z")

</div>

Aggregate a max for a field and then query only those documents which contain the field equal to that aggregated result.

Can we perform this operation so that we get the queried results also rather than giving an aggregation call first and then doing a query call

---

<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:** [May 12, 2021, 10:53am UTC](https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775/2 "2021-05-12T10:53:49Z")

</div>

Use the `max` aggregation, and then within that aggregation try the [top\_hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/search-aggregations-metrics-top-hits-aggregation.html).

Hope that helps!

---

<div class="post-metadata">

**Author:** ![RapTvin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raptvin/32/86995_2.png) [@RapTvin](https://discuss.elastic.co/u/RapTvin)\
**Post date:** [May 12, 2021, 1:33pm UTC](https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775/3 "2021-05-12T13:33:02Z")

</div>

`max` doesn't support sub aggregations @spinscale

---

<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:** [May 12, 2021, 1:40pm UTC](https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775/4 "2021-05-12T13:40:00Z")

</div>

Aggregations happen as part of the search phase (not any subsequent "fetch" phase) so all data gathering is done in isolation of what might be happening on other remote shards - eg finding some global max for a field.  
To do what you want you'd need two search phases so this is something you'd have to do in your client as 2 separate requests:

1. Determine global max value using an agg
2. Search for docs with that max value

---

<div class="post-metadata">

**Author:** ![RapTvin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raptvin/32/86995_2.png) [@RapTvin](https://discuss.elastic.co/u/RapTvin)\
**Post date:** [May 13, 2021, 3:42am UTC](https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775/5 "2021-05-13T03:42:35Z")

</div>

@Mark_Harwood Yes currently I am following the 2 search request pattern but just to optimise it wanted to know if there is any other way to do so but as you have explained now it won't be the case.

---

<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:** [June 10, 2021, 3:43am UTC](https://discuss.elastic.co/t/question-use-aggregation-result-to-query-documents-in-a-single-query-to-optimise-calls-to-es/272775/6 "2021-06-10T03:43:19Z")

</div>

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