11[[search-request-preference]]
22=== Preference
33
4- Controls a `preference` of which shard copies on which to execute the
5- search. By default, the operation is randomized among the available shard
6- copies, unless allocation awareness is used.
4+ Controls a `preference` of the shard copies on which to execute the search. By
5+ default, Elasticsearch selects from the available shard copies in an
6+ unspecified order, taking the <<allocation-awareness,allocation awareness>> and
7+ <<search-adaptive-replica,adaptive replica selection>> configuration into
8+ account. However, it may sometimes be desirable to try and route certain
9+ searches to certain sets of shard copies, for instance to make better use of
10+ per-copy caches.
711
812The `preference` is a query string parameter which can be set to:
913
1014[horizontal]
11- `_local`::
12- The operation will prefer to be executed on a local
13- allocated shard if possible.
15+ `_only_local`::
16+ The operation will be executed only on shards allocated to the local
17+ node.
18+
19+ `_local`::
20+ The operation will be executed on shards allocated to the local node if
21+ possible, and will fall back to other shards if not.
1422
1523`_prefer_nodes:abc,xyz`::
16- Prefers execution on the nodes with the provided
17- node ids (`abc` or `xyz` in this case) if applicable.
24+ The operation will be executed on nodes with one of the provided node
25+ ids (`abc` or `xyz` in this case) if possible. If suitable shard copies
26+ exist on more than one of the selected nodes then the order of
27+ preference between these copies is unspecified.
1828
19- `_shards:2,3`::
20- Restricts the operation to the specified shards. (`2`
21- and `3` in this case). This preference can be combined with other
22- preferences but it has to appear first: `_shards:2,3|_local`
29+ `_shards:2,3`::
30+ Restricts the operation to the specified shards. (`2` and `3` in this
31+ case). This preference can be combined with other preferences but it
32+ has to appear first: `_shards:2,3|_local`
2333
24- `_only_nodes`::
25- Restricts the operation to nodes specified in <<cluster,node specification>>
34+ `_only_nodes:abc*,x*yz,...`::
35+ Restricts the operation to nodes specified according to the
36+ <<cluster,node specification>>. If suitable shard copies exist on more
37+ than one of the selected nodes then the order of preference between
38+ these copies is unspecified.
2639
27- Custom (string) value::
28- A custom value will be used to guarantee that
29- the same shards will be used for the same custom value. This can help
30- with "jumping values" when hitting different shards in different refresh
31- states. A sample value can be something like the web session id, or the
32- user name.
40+ Custom (string) value::
41+ Any value that does not start with `_`. If two searches both give the same
42+ custom string value for their preference and the underlying cluster state
43+ does not change then the same ordering of shards will be used for the
44+ searches. This does not guarantee that the exact same shards will be used
45+ each time: the cluster state, and therefore the selected shards, may change
46+ for a number of reasons including shard relocations and shard failures, and
47+ nodes may sometimes reject searches causing fallbacks to alternative nodes.
48+ However, in practice the ordering of shards tends to remain stable for long
49+ periods of time. A good candidate for a custom preference value is something
50+ like the web session id or the user name.
3351
34- For instance, use the user's session ID to ensure consistent ordering of results
35- for the user:
52+ For instance, use the user's session ID `xyzabc123` as follows:
3653
3754[source,js]
3855------------------------------------------------
@@ -47,3 +64,9 @@ GET /_search?preference=xyzabc123
4764------------------------------------------------
4865// CONSOLE
4966
67+ NOTE: The `_only_local` preference guarantees only to use shard copies on the
68+ local node, which is sometimes useful for troubleshooting. All other options do
69+ not _fully_ guarantee that any particular shard copies are used in a search,
70+ and on a changing index this may mean that repeated searches may yield
71+ different results if they are executed on different shard copies which are in
72+ different refresh states.
0 commit comments