Skip to content

Commit e297457

Browse files
committed
Improve docs for search preferences
Today it is unclear what guarantees are offered by the search preference feature, and we claim a guarantee that is stronger than what we really offer: > A custom value will be used to guarantee that the same shards will be used > for the same custom value. This commit clarifies this documentation and explains more clearly why `_primary`, `_replica`, etc. are deprecated in `6.x` and removed in `master`. Relates elastic#31929 elastic#26335 elastic#26791.
1 parent d4d2df7 commit e297457

File tree

1 file changed

+68
-34
lines changed

1 file changed

+68
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,81 @@
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.
11+
12+
Preferences do not _guarantee_ that any particular shard copies are used in a
13+
search, and on a changing index this may mean that repeated searches may yield
14+
different results if they are executed on different shard copies which are in
15+
different refresh states.
716

817
The `preference` is a query string parameter which can be set to:
918

1019
[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`]
20+
`_primary`::
21+
The operation will be executed only on primary shards.
22+
deprecated[6.1.0, will be removed in 7.0, use `_only_nodes` or
23+
`_prefer_nodes`]
1424

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`]
25+
`_primary_first`::
26+
The operation will be executed on primary shards if possible, but will
27+
fall back to other shards if not. deprecated[6.1.0, will be removed in
28+
7.0, use `_only_nodes` or `_prefer_nodes`]
1929

2030
`_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`]
31+
The operation will be executed only on replica shards. If there are
32+
multiple replicas then the order of preference between them is
33+
unspecified. deprecated[6.1.0, will be removed in 7.0, use
34+
`_only_nodes` or `_prefer_nodes`]
2335

2436
`_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`]
37+
The operation will be executed on replica shards if possible, but will
38+
fall back to other shards if not. If there are multiple replicas then
39+
the order of preference between them is unspecified. deprecated[6.1.0,
40+
will be removed in 7.0, use `_only_nodes` or `_prefer_nodes`]
41+
42+
`_only_local`::
43+
The operation will be executed only on shards allocated to the local
44+
node.
2845

29-
`_local`::
30-
The operation will prefer to be executed on a local
31-
allocated shard if possible.
46+
`_local`::
47+
The operation will be executed on shards allocated to the local node if
48+
possible, and will fall back to other shards if not.
3249

3350
`_prefer_nodes:abc,xyz`::
34-
Prefers execution on the nodes with the provided
35-
node ids (`abc` or `xyz` in this case) if applicable.
51+
The operation will be executed on nodes with one of the provided node
52+
ids (`abc` or `xyz` in this case) if possible. If suitable shard copies
53+
exist on more than one of the selected nodes then the order of
54+
preference between these copies is unspecified.
3655

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`
56+
`_shards:2,3`::
57+
Restricts the operation to the specified shards. (`2` and `3` in this
58+
case). This preference can be combined with other preferences but it
59+
has to appear first: `_shards:2,3|_local`
4160

42-
`_only_nodes`::
43-
Restricts the operation to nodes specified in <<cluster,node specification>>
61+
`_only_nodes:abc*,x*yz,...`::
62+
Restricts the operation to nodes specified according to the
63+
<<cluster,node specification>>. If suitable shard copies exist on more
64+
than one of the selected nodes then the order of preference between
65+
these copies is unspecified.
4466

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.
67+
Custom (string) value::
68+
Any value that does not start with `_`. If two searches both give the same
69+
custom string value for their preference and the underlying cluster state
70+
does not change then the same ordering of shards will be used for the
71+
searches. This does not guarantee that the exact same shards will be used
72+
each time: the cluster state, and therefore the selected shards, may change
73+
for a number of reasons including shard relocations and shard failures, and
74+
nodes may sometimes reject searches causing fallbacks to alternative nodes.
75+
A good candidate for a custom preference value is something like the web
76+
session id or the user name.
5177

52-
For instance, use the user's session ID to ensure consistent ordering of results
53-
for the user:
78+
For instance, use the user's session ID `xyzabc123` as follows:
5479

5580
[source,js]
5681
------------------------------------------------
@@ -65,3 +90,12 @@ GET /_search?preference=xyzabc123
6590
------------------------------------------------
6691
// CONSOLE
6792

93+
WARNING: The `_primary`, `_primary_first`, `_replica` and `_replica_first` are
94+
not recommended, and will be removed in a future version. They do not help to
95+
avoid inconsistent results that arise from the use of shards that have
96+
different refresh states, and Elasticsearch uses synchronous replication so the
97+
primary does not in general hold fresher data than its replicas. The
98+
`_primary_first` and `_replica_first` preferences silently fall back to
99+
non-preferred copies if it is not possible to search the preferred copies. The
100+
`_primary` and `_replica` preferences will silently change their preferred
101+
shards if a replica is promoted to primary, which can happen at any time.

0 commit comments

Comments
 (0)