preference parameter allows
you to control which shards or nodes are
used to handle the search request. It accepts values such as
_shards:2,3, which are explained in detail on the
By default, shards process all the data they have before returning a response to the coordinating node, which will in turn merge these responses to build the final response.
This means that the time it takes to run a search request is the sum of the time it takes to process the slowest shard and the time it takes to merge responses. If one node is having trouble, it could slow down the response to all search requests.
timeout parameter tells shards how long they
are allowed to process data before returning a response to the coordinating
node. If there was not enough time to process all data, results for this shard
will be partial, even possibly empty.
The response to a search request will indicate whether any shards returned a
partial response with the
It’s important to know that the timeout is still a best-effort operation; it’s possible for the query to surpass the allotted timeout. There are two reasons for this behavior:
- Timeout checks are performed on a per-document basis. However, some query types have a significant amount of work that must be performed before documents are evaluated. This "setup" phase does not consult the timeout, and so very long setup times can cause the overall latency to shoot past the timeout.
- Because the time is once per document, a very long query can execute on a single document and it won’t timeout until the next document is evaluated. This also means poorly written scripts (e.g. ones with infinite loops) will be allowed to execute forever.
In Routing a Document to a Shard, we explained how a custom
provided at index time to ensure that all related documents, such as the
documents belonging to a single user, are stored on a single shard. At search
time, instead of searching on all the shards of an index, you can specify
one or more
routing values to limit the search to just those shards:
This technique comes in handy when designing very large search systems, and we discuss it in detail in Designing for Scale.
dfs_query_then_fetch search type has a prequery phase that fetches the term
frequencies from all involved shards to calculate global term
frequencies. We discuss this further in Relevance Is Broken!.