multi-search.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. [[search-multi-search]]
  2. === Multi Search API
  3. Executes several searches with a single API request.
  4. [[search-multi-search-api-request]]
  5. ==== {api-request-title}
  6. `GET /<index>/_msearch`
  7. [[search-multi-search-api-desc]]
  8. ==== {api-description-title}
  9. The multi search AP executes several searches from a single API request.
  10. The format of the request is similar to the bulk API format and makes use
  11. of the newline delimited JSON (NDJSON) format. The structure is as follows (the
  12. structure is specifically optimized to reduce parsing if a specific search ends
  13. up redirected to another node):
  14. [source,js]
  15. --------------------------------------------------
  16. header\n
  17. body\n
  18. header\n
  19. body\n
  20. --------------------------------------------------
  21. // NOTCONSOLE
  22. *NOTE*: the final line of data must end with a newline character `\n`. Each newline character
  23. may be preceded by a carriage return `\r`. When sending requests to this endpoint the
  24. `Content-Type` header should be set to `application/x-ndjson`.
  25. [[search-multi-search-api-path-params]]
  26. ==== {api-path-parms-title}
  27. include::{docdir}/rest-api/common-parms.asciidoc[tag=index]
  28. [[search-multi-search-api-request-body]]
  29. ==== {api-request-body-title}
  30. `aggregations`::
  31. (Optional, object) Specifies aggregations.
  32. include::{docdir}/rest-api/common-parms.asciidoc[tag=from]
  33. `max_concurrent_searches`::
  34. (Optional, integer) Specifies the maximum number of concurrent searches the
  35. multi search API will execute. This default is based on the number of data
  36. nodes and the default search thread pool size.
  37. `max_concurrent_shard_requests`::
  38. (Optional, integer) Specifies the maximum number of concurrent shard requests
  39. that each sub search request will execute per node. This parameter should be
  40. used to protect a single request from overloading a cluster (for example a
  41. default request will hit all indices in a cluster which could cause shard
  42. request rejections if the number of shards per node is high). Defaults to `5`.
  43. In certain scenarios parallelism isn't achieved through concurrent request
  44. such that this protection will result in poor performance. For instance in an
  45. environment where only a very low number of concurrent search requests are
  46. expected it might help to increase this value to a higher number.
  47. include::{docdir}/rest-api/common-parms.asciidoc[tag=preference]
  48. `query`::
  49. (Optional, <<query-dsl,query object>>) Defines the search definition using the
  50. <<query-dsl,Query DSL>>.
  51. include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]
  52. include::{docdir}/rest-api/common-parms.asciidoc[tag=search_type]
  53. `size`::
  54. (Optional, integer) The number of hits to return. Defaults to `10`.
  55. [[search-multi-search-api-response-body]]
  56. ==== {api-response-body-title}
  57. `responses`::
  58. (array) Includes the search response and status code for each search request
  59. matching its order in the original multi search request. If there was a
  60. complete failure for a specific search request, an object with `error` message
  61. and corresponding status code will be returned in place of the actual search
  62. response.
  63. [[search-multi-search-api-example]]
  64. ==== {api-examples-title}
  65. The header part includes which index / indices to search on, the `search_type`,
  66. `preference`, and `routing`. The body includes the typical search body request
  67. (including the `query`, `aggregations`, `from`, `size`, and so on).
  68. [source,js]
  69. --------------------------------------------------
  70. $ cat requests
  71. {"index" : "test"}
  72. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  73. {"index" : "test", "search_type" : "dfs_query_then_fetch"}
  74. {"query" : {"match_all" : {}}}
  75. {}
  76. {"query" : {"match_all" : {}}}
  77. {"query" : {"match_all" : {}}}
  78. {"search_type" : "dfs_query_then_fetch"}
  79. {"query" : {"match_all" : {}}}
  80. --------------------------------------------------
  81. // NOTCONSOLE
  82. [source,js]
  83. --------------------------------------------------
  84. $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch --data-binary "@requests"; echo
  85. --------------------------------------------------
  86. // NOTCONSOLE
  87. Note, the above includes an example of an empty header (can also be just
  88. without any content) which is supported as well.
  89. The endpoint allows to also search against an index/indices in the URI itself,
  90. in which case it will be used as the default unless explicitly defined otherwise
  91. in the header. For example:
  92. [source,console]
  93. --------------------------------------------------
  94. GET twitter/_msearch
  95. {}
  96. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  97. {}
  98. {"query" : {"match_all" : {}}}
  99. {"index" : "twitter2"}
  100. {"query" : {"match_all" : {}}}
  101. --------------------------------------------------
  102. // TEST[setup:twitter]
  103. The above will execute the search against the `twitter` index for all the
  104. requests that don't define an index, and the last one will be executed
  105. against the `twitter2` index.
  106. The `search_type` can be set in a similar manner to globally apply to
  107. all search requests.
  108. [[msearch-security]]
  109. ==== Security
  110. See <<url-access-control>>
  111. [[template-msearch]]
  112. ==== Template support
  113. Much like described in <<search-template>> for the _search resource, _msearch
  114. also provides support for templates. Submit them like follows for inline
  115. templates:
  116. [source,console]
  117. -----------------------------------------------
  118. GET _msearch/template
  119. {"index" : "twitter"}
  120. { "source" : "{ \"query\": { \"match\": { \"message\" : \"{{keywords}}\" } } } }", "params": { "query_type": "match", "keywords": "some message" } }
  121. {"index" : "twitter"}
  122. { "source" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
  123. -----------------------------------------------
  124. // TEST[setup:twitter]
  125. You can also create search templates:
  126. [source,console]
  127. ------------------------------------------
  128. POST /_scripts/my_template_1
  129. {
  130. "script": {
  131. "lang": "mustache",
  132. "source": {
  133. "query": {
  134. "match": {
  135. "message": "{{query_string}}"
  136. }
  137. }
  138. }
  139. }
  140. }
  141. ------------------------------------------
  142. // TEST[setup:twitter]
  143. [source,console]
  144. ------------------------------------------
  145. POST /_scripts/my_template_2
  146. {
  147. "script": {
  148. "lang": "mustache",
  149. "source": {
  150. "query": {
  151. "term": {
  152. "{{field}}": "{{value}}"
  153. }
  154. }
  155. }
  156. }
  157. }
  158. ------------------------------------------
  159. // TEST[continued]
  160. You can use search templates in a _msearch:
  161. [source,console]
  162. -----------------------------------------------
  163. GET _msearch/template
  164. {"index" : "main"}
  165. { "id": "my_template_1", "params": { "query_string": "some message" } }
  166. {"index" : "main"}
  167. { "id": "my_template_2", "params": { "field": "user", "value": "test" } }
  168. -----------------------------------------------
  169. // TEST[continued]
  170. [[multi-search-partial-responses]]
  171. ==== Partial responses
  172. To ensure fast responses, the multi search API will respond with partial results
  173. if one or more shards fail. See <<shard-failures, Shard failures>> for more
  174. information.