multi-search.asciidoc 11 KB

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