multi-search.asciidoc 11 KB

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