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 copies.
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.
611
712The `preference` is a query string parameter which can be set to:
813
914[horizontal]
10- `_primary`::
11- The operation will go and be executed only on the primary
12- 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. ]
1318
14- `_primary_first`::
15- The operation will go and be executed on the primary
16- shard, and if not available (failover) , will execute on other shards.
17- 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. ]
1823
1924`_replica`::
20- The operation will go and be executed only on a replica shard.
21- 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.]
2229
2330`_replica_first`::
24- The operation will go and be executed only on a replica shard, and if
25- not available (failover), will execute on other shards.
26- 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.]
2735
28- `_local`::
29- The operation will prefer to be executed on a local
30- 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.
3143
3244`_prefer_nodes:abc,xyz`::
33- Prefers execution on the nodes with the provided
34- 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.
3549
36- `_shards:2,3`::
37- Restricts the operation to the specified shards. (`2`
38- and `3` in this case). This preference can be combined with other
39- 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`
4054
41- `_only_nodes`::
42- 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.
4360
44- Custom (string) value::
45- A custom value will be used to guarantee that
46- the same shards will be used for the same custom value. This can help
47- with "jumping values" when hitting different shards in different refresh
48- states. A sample value can be something like the web session id, or the
49- 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.
5072
51- For instance, use the user's session ID to ensure consistent ordering of results
52- for the user:
73+ For instance, use the user's session ID `xyzabc123` as follows:
5374
5475[source,js]
5576------------------------------------------------
@@ -64,3 +85,22 @@ GET /_search?preference=xyzabc123
6485------------------------------------------------
6586// CONSOLE
6687
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