preference.asciidoc 3.0 KB

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