The batched query phase can cut search execution time in half by reducing transport overhead and better distributing reduction work across the cluster. The batched query phase, which is live in Elasticsearch Serverless and released in Elasticsearch 9. 0, changes how search is coordinated across the cluster. The coordinating node now batches all queries for each data node into one, instead of a separate transport request per shard. Data nodes can then do partial result reduction themselves, rather than shipping everything back to the coordinating node.

In coordination-bound workloads, this can cut search execution time in half. Let’s start with some Elasticsearch basics. When a search request lands on an Elasticsearch node, that node is called the coordinating node for the search. By default, any Elasticsearch node can act as a coordinating node. The coordinating node determines which shards need to be searched based on the index or indices specified by the search. The nodes that those shards live on are called data nodes . A coordinating node can also be a data node, as it may host shards that are relevant to the search.

Elasticsearch performs the search in two primary phases: the query phase (also known as the scatter phase ) and the fetch phase (also known as the gather phase ). The query phase is responsible for going to the data nodes and executing the query on each shard. Each shard responds with a set of document IDs (just the IDs, no data) and an associated score for each. These results are reduced. Next, the fetch phase goes back out to the data nodes to fetch the document _source (the data). What exactly is a reduction in Elasticsearch?

A reduction turns per-shard results from the query phase into a single merged result for the client. Suppose a search asks for the top five hits in a three-shard index, according to some relevance score. Elasticsearch must then get the top five hits from each targeted shard. Because it’s possible that one shard contains the global top five, or, more likely, that the top five docs are spread across shards. If three shards are being searched, the coordinating node will have 15 (docID, score) pairs after the query phase.