preference.asciidoc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [[request-body-search-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 <<shard-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.
  9. A possible use case would be to make use of per-copy caches like the
  10. <<shard-request-cache,request cache>>. Doing this, however, runs contrary to the
  11. idea of search parallelization and can create hotspots on certain nodes because
  12. the load might not be evenly distributed anymore.
  13. The `preference` is a query string parameter which can be set to:
  14. [horizontal]
  15. `_only_local`::
  16. The operation will be executed only on shards allocated to the local
  17. node.
  18. `_local`::
  19. The operation will be executed on shards allocated to the local node if
  20. possible, and will fall back to other shards if not.
  21. `_prefer_nodes:abc,xyz`::
  22. The operation will be executed on nodes with one of the provided node
  23. ids (`abc` or `xyz` in this case) if possible. If suitable shard copies
  24. exist on more than one of the selected nodes then the order of
  25. preference between these copies is unspecified.
  26. `_shards:2,3`::
  27. Restricts the operation to the specified shards. (`2` and `3` in this
  28. case). This preference can be combined with other preferences but it
  29. has to appear first: `_shards:2,3|_local`
  30. `_only_nodes:abc*,x*yz,...`::
  31. Restricts the operation to nodes specified according to the
  32. <<cluster,node specification>>. If suitable shard copies exist on more
  33. than one of the selected nodes then the order of preference between
  34. these copies is unspecified.
  35. Custom (string) value::
  36. Any value that does not start with `_`. If two searches both give the same
  37. custom string value for their preference and the underlying cluster state
  38. does not change then the same ordering of shards will be used for the
  39. searches. This does not guarantee that the exact same shards will be used
  40. each time: the cluster state, and therefore the selected shards, may change
  41. for a number of reasons including shard relocations and shard failures, and
  42. nodes may sometimes reject searches causing fallbacks to alternative nodes.
  43. However, in practice the ordering of shards tends to remain stable for long
  44. periods of time. A good candidate for a custom preference value is something
  45. like the web session id or the user name.
  46. For instance, use the user's session ID `xyzabc123` as follows:
  47. [source,console]
  48. ------------------------------------------------
  49. GET /_search?preference=xyzabc123
  50. {
  51. "query": {
  52. "match": {
  53. "title": "elasticsearch"
  54. }
  55. }
  56. }
  57. ------------------------------------------------
  58. This can be an effective strategy to increase usage of e.g. the request cache for
  59. unique users running similar searches repeatedly by always hitting the same cache, while
  60. requests of different users are still spread across all shard copies.
  61. NOTE: The `_only_local` preference guarantees only to use shard copies on the
  62. local node, which is sometimes useful for troubleshooting. All other options do
  63. not _fully_ guarantee that any particular shard copies are used in a search,
  64. and on a changing index this may mean that repeated searches may yield
  65. different results if they are executed on different shard copies which are in
  66. different refresh states.