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 which shard copies on which to execute the search.
5+ By 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- `_primary`::
12- The operation will go and be executed only on the primary
13- shards. deprecated[6.1.0, will be removed in 7.0, use `_only_nodes` or `_prefer_nodes` ]
15+ `_primary`::
16+ The operation will be executed only on primary shards. deprecated[6.1.0,
17+ will be removed in 7.0. See the warning below for more information. ]
1418
15- `_primary_first`::
16- The operation will go and be executed on the primary
17- shard, and if not available (failover) , will execute on other shards.
18- deprecated[6.1.0, will be removed in 7.0, use `_only_nodes` or `_prefer_nodes` ]
19+ `_primary_first`::
20+ The operation will be executed on primary shards if possible, but will fall
21+ back to other shards if not. deprecated[6.1.0 , will be removed in 7.0. See
22+ the warning below for more information. ]
1923
2024`_replica`::
21- The operation will go and be executed only on a replica shard.
22- deprecated[6.1.0, will be removed in 7.0, use `_only_nodes` or `_prefer_nodes`]
25+ The operation will be executed only on replica shards. If there are multiple
26+ replicas then the order of preference between them is unspecified.
27+ deprecated[6.1.0, will be removed in 7.0. See the warning below for more
28+ information.]
2329
2430`_replica_first`::
25- The operation will go and be executed only on a replica shard, and if
26- not available (failover), will execute on other shards.
27- deprecated[6.1.0, will be removed in 7.0, use `_only_nodes` or `_prefer_nodes`]
31+ The operation will be executed on replica shards if possible, but will fall
32+ back to other shards if not. If there are multiple replicas then the order of
33+ preference between them is unspecified. deprecated[6.1.0, will be removed in
34+ 7.0. See the warning below for more information.]
2835
29- `_local`::
30- The operation will prefer to be executed on a local
31- allocated shard if possible.
36+ `_only_local`::
37+ The operation will be executed only on shards allocated to the local
38+ node.
39+
40+ `_local`::
41+ The operation will be executed on shards allocated to the local node if
42+ possible, and will fall back to other shards if not.
3243
3344`_prefer_nodes:abc,xyz`::
34- Prefers execution on the nodes with the provided
35- node ids (`abc` or `xyz` in this case) if applicable.
45+ The operation will be executed on nodes with one of the provided node
46+ ids (`abc` or `xyz` in this case) if possible. If suitable shard copies
47+ exist on more than one of the selected nodes then the order of
48+ preference between these copies is unspecified.
3649
37- `_shards:2,3`::
38- Restricts the operation to the specified shards. (`2`
39- and `3` in this case). This preference can be combined with other
40- preferences but it has to appear first: `_shards:2,3|_local`
50+ `_shards:2,3`::
51+ Restricts the operation to the specified shards. (`2` and `3` in this
52+ case). This preference can be combined with other preferences but it
53+ has to appear first: `_shards:2,3|_local`
4154
42- `_only_nodes`::
43- Restricts the operation to nodes specified in <<cluster,node specification>>
55+ `_only_nodes:abc*,x*yz,...`::
56+ Restricts the operation to nodes specified according to the
57+ <<cluster,node specification>>. If suitable shard copies exist on more
58+ than one of the selected nodes then the order of preference between
59+ these copies is unspecified.
4460
45- Custom (string) value::
46- A custom value will be used to guarantee that
47- the same shards will be used for the same custom value. This can help
48- with "jumping values" when hitting different shards in different refresh
49- states. A sample value can be something like the web session id, or the
50- user name.
61+ Custom (string) value::
62+ Any value that does not start with `_`. If two searches both give the same
63+ custom string value for their preference and the underlying cluster state
64+ does not change then the same ordering of shards will be used for the
65+ searches. This does not guarantee that the exact same shards will be used
66+ each time: the cluster state, and therefore the selected shards, may change
67+ for a number of reasons including shard relocations and shard failures, and
68+ nodes may sometimes reject searches causing fallbacks to alternative nodes.
69+ However, in practice the ordering of shards tends to remain stable for long
70+ periods of time. A good candidate for a custom preference value is something
71+ like the web session id or the user name.
5172
52- For instance, use the user's session ID to ensure consistent ordering of results
53- for the user:
73+ For instance, use the user's session ID `xyzabc123` as follows:
5474
5575[source,js]
5676------------------------------------------------
@@ -65,3 +85,22 @@ GET /_search?preference=xyzabc123
6585------------------------------------------------
6686// CONSOLE
6787
88+ NOTE: The `_only_local` preference guarantees only to use shard copies on the
89+ local node, which is sometimes useful for troubleshooting. All other options do
90+ not _fully_ guarantee that any particular shard copies are used in a search,
91+ and on a changing index this may mean that repeated searches may yield
92+ different results if they are executed on different shard copies which are in
93+ different refresh states.
94+
95+ WARNING: The `_primary`, `_primary_first`, `_replica` and `_replica_first` are
96+ deprecated as their use is not recommended. They do not help to avoid
97+ inconsistent results that arise from the use of shards that have different
98+ refresh states, and Elasticsearch uses synchronous replication so the primary
99+ does not in general hold fresher data than its replicas. The `_primary_first`
100+ and `_replica_first` preferences silently fall back to non-preferred copies if
101+ it is not possible to search the preferred copies. The `_primary` and
102+ `_replica` preferences will silently change their preferred shards if a replica
103+ is promoted to primary, which can happen at any time. The `_primary` preference
104+ can also put undesirable extra load on the primary shards. The cache-related
105+ benefits of these options can also be obtained using `_only_nodes`,
106+ `_prefer_nodes`, or a custom string value instead.
0 commit comments