search.asciidoc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. [[search]]
  2. = Search APIs
  3. [partintro]
  4. --
  5. Most search APIs are <<search-multi-index,multi-index>>, with the
  6. exception of the <<search-explain>> endpoints.
  7. [float]
  8. [[search-routing]]
  9. == Routing
  10. When executing a search, Elasticsearch will pick the "best" copy of the data
  11. based on the <<search-adaptive-replica,adaptive replica selection>> formula.
  12. Which shards will be searched on can also be controlled by providing the
  13. `routing` parameter. For example, when indexing tweets, the routing value can be
  14. the user name:
  15. [source,js]
  16. --------------------------------------------------
  17. POST /twitter/_doc?routing=kimchy
  18. {
  19. "user" : "kimchy",
  20. "postDate" : "2009-11-15T14:12:12",
  21. "message" : "trying out Elasticsearch"
  22. }
  23. --------------------------------------------------
  24. // CONSOLE
  25. In such a case, if we want to search only on the tweets for a specific
  26. user, we can specify it as the routing, resulting in the search hitting
  27. only the relevant shard:
  28. [source,js]
  29. --------------------------------------------------
  30. POST /twitter/_search?routing=kimchy
  31. {
  32. "query": {
  33. "bool" : {
  34. "must" : {
  35. "query_string" : {
  36. "query" : "some query string here"
  37. }
  38. },
  39. "filter" : {
  40. "term" : { "user" : "kimchy" }
  41. }
  42. }
  43. }
  44. }
  45. --------------------------------------------------
  46. // CONSOLE
  47. // TEST[continued]
  48. The routing parameter can be multi valued represented as a comma
  49. separated string. This will result in hitting the relevant shards where
  50. the routing values match to.
  51. [float]
  52. [[search-adaptive-replica]]
  53. == Adaptive Replica Selection
  54. By default, Elasticsearch will use what is called adaptive replica selection.
  55. This allows the coordinating node to send the request to the copy deemed "best"
  56. based on a number of criteria:
  57. - Response time of past requests between the coordinating node and the node
  58. containing the copy of the data
  59. - Time past search requests took to execute on the node containing the data
  60. - The queue size of the search threadpool on the node containing the data
  61. This can be turned off by changing the dynamic cluster setting
  62. `cluster.routing.use_adaptive_replica_selection` from `true` to `false`:
  63. [source,js]
  64. --------------------------------------------------
  65. PUT /_cluster/settings
  66. {
  67. "transient": {
  68. "cluster.routing.use_adaptive_replica_selection": false
  69. }
  70. }
  71. --------------------------------------------------
  72. // CONSOLE
  73. If adaptive replica selection is turned off, searches are sent to the
  74. index/indices shards in a round robin fashion between all copies of the data
  75. (primaries and replicas).
  76. [float]
  77. [[stats-groups]]
  78. == Stats Groups
  79. A search can be associated with stats groups, which maintains a
  80. statistics aggregation per group. It can later be retrieved using the
  81. <<indices-stats,indices stats>> API
  82. specifically. For example, here is a search body request that associate
  83. the request with two different groups:
  84. [source,js]
  85. --------------------------------------------------
  86. POST /_search
  87. {
  88. "query" : {
  89. "match_all" : {}
  90. },
  91. "stats" : ["group1", "group2"]
  92. }
  93. --------------------------------------------------
  94. // CONSOLE
  95. // TEST[setup:twitter]
  96. [float]
  97. [[global-search-timeout]]
  98. == Global Search Timeout
  99. Individual searches can have a timeout as part of the
  100. <<search-request-body>>. Since search requests can originate from many
  101. sources, Elasticsearch has a dynamic cluster-level setting for a global
  102. search timeout that applies to all search requests that do not set a
  103. timeout in the request body. These requests will be cancelled after
  104. the specified time using the mechanism described in the following section on
  105. <<global-search-cancellation>>. Therefore the same caveats about timeout
  106. responsiveness apply.
  107. The setting key is `search.default_search_timeout` and can be set using the
  108. <<cluster-update-settings>> endpoints. The default value is no global timeout.
  109. Setting this value to `-1` resets the global search timeout to no timeout.
  110. [float]
  111. [[global-search-cancellation]]
  112. == Search Cancellation
  113. Searches can be cancelled using standard <<task-cancellation,task cancellation>>
  114. mechanism. By default, a running search only checks if it is cancelled or
  115. not on segment boundaries, therefore the cancellation can be delayed by large
  116. segments. The search cancellation responsiveness can be improved by setting
  117. the dynamic cluster-level setting `search.low_level_cancellation` to `true`.
  118. However, it comes with an additional overhead of more frequent cancellation
  119. checks that can be noticeable on large fast running search queries. Changing this
  120. setting only affects the searches that start after the change is made.
  121. [float]
  122. [[search-concurrency-and-parallelism]]
  123. == Search concurrency and parallelism
  124. By default Elasticsearch doesn't reject any search requests based on the number
  125. of shards the request hits. While Elasticsearch will optimize the search
  126. execution on the coordinating node a large number of shards can have a
  127. significant impact CPU and memory wise. It is usually a better idea to organize
  128. data in such a way that there are fewer larger shards. In case you would like to
  129. configure a soft limit, you can update the `action.search.shard_count.limit`
  130. cluster setting in order to reject search requests that hit too many shards.
  131. The request parameter `max_concurrent_shard_requests` can be used to control the
  132. maximum number of concurrent shard requests the search API will execute for the
  133. request. This parameter should be used to protect a single request from
  134. overloading a cluster (e.g., a default request will hit all indices in a cluster
  135. which could cause shard request rejections if the number of shards per node is
  136. high). This default is based on the number of data nodes in the cluster but at
  137. most `256`.
  138. --
  139. include::search/search.asciidoc[]
  140. include::search/uri-request.asciidoc[]
  141. include::search/request-body.asciidoc[]
  142. include::search/search-template.asciidoc[]
  143. include::search/search-shards.asciidoc[]
  144. include::search/suggesters.asciidoc[]
  145. include::search/multi-search.asciidoc[]
  146. include::search/count.asciidoc[]
  147. include::search/validate.asciidoc[]
  148. include::search/explain.asciidoc[]
  149. include::search/profile.asciidoc[]
  150. include::search/field-caps.asciidoc[]
  151. include::search/rank-eval.asciidoc[]