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

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

Use the max aggregation, and then within that aggregation try the top_hits aggregation.

Hope that helps!

max doesn't support sub aggregations @spinscale

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

@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.

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