multi-search.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. [[search-multi-search]]
  2. === Multi search API
  3. ++++
  4. <titleabbrev>Multi search</titleabbrev>
  5. ++++
  6. Executes several searches with a single API request.
  7. [source,console]
  8. --------------------------------------------------
  9. GET my-index-000001/_msearch
  10. { }
  11. {"query" : {"match" : { "message": "this is a test"}}}
  12. {"index": "my-index-000002"}
  13. {"query" : {"match_all" : {}}}
  14. --------------------------------------------------
  15. // TEST[setup:my_index]
  16. [[search-multi-search-api-request]]
  17. ==== {api-request-title}
  18. `GET /<target>/_msearch`
  19. [[search-multi-search-api-desc]]
  20. ==== {api-description-title}
  21. The multi search API executes several searches from a single API request.
  22. The format of the request is similar to the bulk API format and makes use
  23. of the newline delimited JSON (NDJSON) format.
  24. The structure is as follows:
  25. [source,js]
  26. --------------------------------------------------
  27. header\n
  28. body\n
  29. header\n
  30. body\n
  31. --------------------------------------------------
  32. // NOTCONSOLE
  33. This structure is specifically optimized to reduce parsing if a specific search
  34. ends up redirected to another node.
  35. [IMPORTANT]
  36. ====
  37. The final line of data must end with a newline character `\n`. Each newline
  38. character may be preceded by a carriage return `\r`. When sending requests to
  39. this endpoint the `Content-Type` header should be set to `application/x-ndjson`.
  40. ====
  41. [[search-multi-search-api-path-params]]
  42. ==== {api-path-parms-title}
  43. `<target>`::
  44. (Optional, string)
  45. Comma-separated list of data streams, indices, and index aliases to search.
  46. +
  47. This list acts as a fallback if a search in the request body does not specify an
  48. `index` target.
  49. +
  50. Wildcard (`*`) expressions are supported. To search all data streams and indices
  51. in a cluster, omit this parameter or use `_all` or `*`.
  52. [[search-multi-search-api-query-params]]
  53. ==== {api-query-parms-title}
  54. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  55. `ccs_minimize_roundtrips`::
  56. (Optional, Boolean)
  57. If `true`, network roundtrips between the coordinating node and remote clusters
  58. are minimized for {ccs} requests. Defaults to `true`. See
  59. <<ccs-network-delays>>.
  60. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  61. +
  62. Defaults to `open`.
  63. `ignore_throttled`::
  64. (Optional, Boolean)
  65. If `true`, concrete, expanded or aliased indices are ignored when frozen.
  66. Defaults to `true`.
  67. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  68. `max_concurrent_searches`::
  69. (Optional, integer)
  70. Maximum number of concurrent searches the multi search API can execute. Defaults
  71. to +max(1, (# of <<data-node,data nodes>> * min(<<search-threadpool,search thread pool size>>, 10)))+.
  72. `max_concurrent_shard_requests`::
  73. +
  74. --
  75. (Optional, integer)
  76. Maximum number of concurrent shard requests that each sub-search request
  77. executes per node. Defaults to `5`.
  78. You can use this parameter to prevent a request from overloading a cluster. For
  79. example, a default request hits all data streams and indices in a cluster. This
  80. could cause shard request rejections if the number of shards per node is high.
  81. In certain scenarios, parallelism isn't achieved through concurrent requests. In
  82. those cases, a low value in this parameter could result in poor performance.
  83. For example, in an environment where a very low number of concurrent search
  84. requests are expected, a higher value in this parameter may improve performance.
  85. --
  86. `pre_filter_shard_size`::
  87. (Optional, integer)
  88. Defines a threshold that enforces a pre-filter roundtrip to prefilter search
  89. shards based on query rewriting if the number of shards the search request
  90. expands to exceeds the threshold. This filter roundtrip can limit the number of
  91. shards significantly if for instance a shard can not match any documents based
  92. on its rewrite method i.e., if date filters are mandatory to match but the
  93. shard bounds and the query are disjoint.
  94. When unspecified, the pre-filter phase is executed if any of these
  95. conditions is met:
  96. - The request targets more than `128` shards.
  97. - The request targets one or more read-only index.
  98. - The primary sort of the query targets an indexed field.
  99. `rest_total_hits_as_int`::
  100. (Optional, Boolean)
  101. If `true`, `hits.total` are returned as an integer in the
  102. response. Defaults to `false`, which returns an object.
  103. `routing`::
  104. (Optional, string)
  105. Custom <<mapping-routing-field,routing value>> used to route search operations
  106. to a specific shard.
  107. `search_type`::
  108. +
  109. --
  110. (Optional, string)
  111. Indicates whether global term and document frequencies should be used when
  112. scoring returned documents.
  113. Options are:
  114. `query_then_fetch`::
  115. (default)
  116. Documents are scored using local term and document frequencies for the shard.
  117. This is usually faster but less accurate.
  118. `dfs_query_then_fetch`::
  119. Documents are scored using global term and document frequencies across all
  120. shards. This is usually slower but more accurate.
  121. --
  122. `typed_keys`::
  123. (Optional, Boolean)
  124. Specifies whether aggregation and suggester names should be prefixed by their
  125. respective types in the response.
  126. [[search-multi-search-api-request-body]]
  127. ==== {api-request-body-title}
  128. The request body contains a newline-delimited list of search `<header>` and
  129. search `<body>` objects.
  130. `<header>`::
  131. +
  132. --
  133. (Required, object)
  134. Contains parameters used to limit or change the subsequent search body request.
  135. This object is required for each search body but can be empty (`{}`) or a blank
  136. line.
  137. --
  138. `allow_no_indices`:::
  139. (Optional, Boolean)
  140. If `true`, the request does *not* return an error if a wildcard expression or
  141. `_all` value retrieves only missing or closed indices.
  142. +
  143. This parameter also applies to <<indices-aliases,index aliases>> that point to a
  144. missing or closed index.
  145. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  146. +
  147. Defaults to `open`.
  148. `ignore_unavailable`:::
  149. (Optional, Boolean) If `true`, documents from missing or closed indices are not
  150. included in the response. Defaults to `false`.
  151. `index`:::
  152. (Optional, string or array of strings)
  153. Data streams, indices, and index aliases to search. Wildcard (`*`) expressions
  154. are supported. You can specify multiple targets as an array.
  155. +
  156. If this parameter is not specified, the `<target>` request path parameter
  157. is used as a fallback.
  158. `preference`:::
  159. (Optional, string)
  160. Node or shard used to perform the search. Random by default.
  161. `request_cache`:::
  162. (Optional, Boolean)
  163. If `true`, the request cache can be used for this search. Defaults to
  164. index-level settings. See <<shard-request-cache>>.
  165. `routing`:::
  166. (Optional, string)
  167. Custom <<mapping-routing-field,routing value>> used to route search operations
  168. to a specific shard.
  169. `search_type`:::
  170. +
  171. --
  172. (Optional, string)
  173. Indicates whether global term and document frequencies should be used when
  174. scoring returned documents.
  175. Options are:
  176. `query_then_fetch`::
  177. (default)
  178. Documents are scored using local term and document frequencies for the shard.
  179. This is usually faster but less accurate.
  180. `dfs_query_then_fetch`::
  181. Documents are scored using global term and document frequencies across all
  182. shards. This is usually slower but more accurate.
  183. --
  184. `<body>`::
  185. (Optional, object)
  186. Contains parameters for a search request:
  187. `aggregations`:::
  188. (Optional, <<search-aggregations,aggregation object>>)
  189. Aggregations you wish to run during the search. See <<search-aggregations>>.
  190. `query`:::
  191. (Optional, <<query-dsl,query DSL object>>) Query you wish to run during the
  192. search. Hits matching this query are returned in the response.
  193. `from`:::
  194. (Optional, integer)
  195. Starting offset for returned hits. Defaults to `0`.
  196. `size`:::
  197. (Optional, integer)
  198. Number of hits to return. Defaults to `10`.
  199. [[search-multi-search-api-response-body]]
  200. ==== {api-response-body-title}
  201. `responses`::
  202. (array) Includes the search response and status code for each search request
  203. matching its order in the original multi search request. If there was a
  204. complete failure for a specific search request, an object with `error` message
  205. and corresponding status code will be returned in place of the actual search
  206. response.
  207. [[search-multi-search-api-example]]
  208. ==== {api-examples-title}
  209. The header part includes which data streams, indices, and index aliases to
  210. search. The header also indicates the `search_type`,
  211. `preference`, and `routing`. The body includes the typical search body request
  212. (including the `query`, `aggregations`, `from`, `size`, and so on).
  213. [source,js]
  214. --------------------------------------------------
  215. $ cat requests
  216. {"index" : "test"}
  217. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  218. {"index" : "test", "search_type" : "dfs_query_then_fetch"}
  219. {"query" : {"match_all" : {}}}
  220. {}
  221. {"query" : {"match_all" : {}}}
  222. {"query" : {"match_all" : {}}}
  223. {"search_type" : "dfs_query_then_fetch"}
  224. {"query" : {"match_all" : {}}}
  225. --------------------------------------------------
  226. // NOTCONSOLE
  227. [source,js]
  228. --------------------------------------------------
  229. $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch --data-binary "@requests"; echo
  230. --------------------------------------------------
  231. // NOTCONSOLE
  232. Note, the above includes an example of an empty header (can also be just
  233. without any content) which is supported as well.
  234. The endpoint also allows you to search against data streams, indices, and index
  235. aliases in the request path. In this case, it will be used as the default target
  236. unless explicitly specified in the header's `index` parameter. For example:
  237. [source,console]
  238. --------------------------------------------------
  239. GET my-index-000001/_msearch
  240. {}
  241. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  242. {}
  243. {"query" : {"match_all" : {}}}
  244. {"index" : "my-index-000002"}
  245. {"query" : {"match_all" : {}}}
  246. --------------------------------------------------
  247. // TEST[setup:my_index]
  248. The above will execute the search against the `my-index-000001` index for all the
  249. requests that don't define an `index` target in the request body. The last
  250. search will be executed against the `my-index-000002` index.
  251. The `search_type` can be set in a similar manner to globally apply to
  252. all search requests.
  253. [[msearch-security]]
  254. ==== Security
  255. See <<url-access-control>>
  256. [[template-msearch]]
  257. ==== Template support
  258. Much like described in <<search-template>> for the _search resource, _msearch
  259. also provides support for templates. Submit them like follows for inline
  260. templates:
  261. [source,console]
  262. -----------------------------------------------
  263. GET _msearch/template
  264. {"index" : "my-index-000001"}
  265. { "source" : "{ \"query\": { \"match\": { \"message\" : \"{{keywords}}\" } } } }", "params": { "query_type": "match", "keywords": "some message" } }
  266. {"index" : "my-index-000001"}
  267. { "source" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
  268. -----------------------------------------------
  269. // TEST[setup:my_index]
  270. You can also create search templates:
  271. [source,console]
  272. ------------------------------------------
  273. POST /_scripts/my_template_1
  274. {
  275. "script": {
  276. "lang": "mustache",
  277. "source": {
  278. "query": {
  279. "match": {
  280. "message": "{{query_string}}"
  281. }
  282. }
  283. }
  284. }
  285. }
  286. ------------------------------------------
  287. // TEST[setup:my_index]
  288. [source,console]
  289. ------------------------------------------
  290. POST /_scripts/my_template_2
  291. {
  292. "script": {
  293. "lang": "mustache",
  294. "source": {
  295. "query": {
  296. "term": {
  297. "{{field}}": "{{value}}"
  298. }
  299. }
  300. }
  301. }
  302. }
  303. ------------------------------------------
  304. // TEST[continued]
  305. You can use search templates in a _msearch:
  306. [source,console]
  307. -----------------------------------------------
  308. GET _msearch/template
  309. {"index" : "main"}
  310. { "id": "my_template_1", "params": { "query_string": "some message" } }
  311. {"index" : "main"}
  312. { "id": "my_template_2", "params": { "field": "user", "value": "test" } }
  313. -----------------------------------------------
  314. // TEST[continued]
  315. [[multi-search-partial-responses]]
  316. ==== Partial responses
  317. To ensure fast responses, the multi search API will respond with partial results
  318. if one or more shards fail. See <<shard-failures, Shard failures>> for more
  319. information.
  320. [[msearch-cancellation]]
  321. ==== Search Cancellation
  322. Multi searches can be cancelled using standard <<task-cancellation,task cancellation>>
  323. mechanism and are also automatically cancelled when the http connection used to
  324. perform the request is closed by the client. It is fundamental that the http
  325. client sending requests closes connections whenever requests time out or are
  326. aborted. Cancelling an msearch request will also cancel all of the corresponding
  327. sub search requests.