elastic / kibana

Your window into the Elastic Stack
https://www.elastic.co/products/kibana
Other
19.35k stars 7.98k forks source link

Unnecessary multiple requests with the same esagg query #182919

Closed ppisljar closed 6 days ago

ppisljar commented 1 week ago

Unnecessary multiple requests with the same esagg query

When a search is sent to ES and a response is received, the search service is looking if the request needs a post-flight request. https://github.com/elastic/kibana/blob/74fdd1b5f25c783ef95b3fddb2eaef03ff597345/src/plugins/data/common/search/search_source/search_source.ts#L539-L548 If needed, it transforms the response to a partial response and update the body with the postflight request. This works correctly if the postflight is actually necessary, but due to the current implementation the postflight request is always "applied" even if not needed, causing a subsequent request to be sent to ES. This results to an increase of:

Analysis The current method that checks if a request needs a subsequent post-flight request relies on a loose check from the function hasPostFlightRequests. This function checks if the agg property type.postFlightRequest is a function.

https://github.com/elastic/kibana/blob/74fdd1b5f25c783ef95b3fddb2eaef03ff597345/src/plugins/data/common/search/search_source/search_source.ts#L474-L483

This function is there even if is not required. For example in a terms aggregation without the other bucket the function is still there but just return its identity https://github.com/elastic/kibana/blob/74fdd1b5f25c783ef95b3fddb2eaef03ff597345/src/plugins/data/common/search/aggs/buckets/terms.ts#L93 All the other cases this is defaulted to an identity function, so the hasPostFlightRequests function will always return true. https://github.com/elastic/kibana/blob/74fdd1b5f25c783ef95b3fddb2eaef03ff597345/src/plugins/data/common/search/aggs/agg_type.ts#L311

elasticmachine commented 1 week ago

Pinging @elastic/kibana-visualizations (Team:Visualizations)