search.asciidoc 5.8 KB

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