multi-search.asciidoc 13 KB

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