[[multi-search-template]] === Multi search template API ++++ Multi search template ++++ Runs multiple <> with a single request. //// [source,console] ---- PUT _scripts/my-search-template { "script": { "lang": "mustache", "source": { "query": { "match": { "message": "{{query_string}}" } }, "from": "{{from}}", "size": "{{size}}" }, "params": { "query_string": "My query string" } } } PUT my-index/_doc/1?refresh { "message": "hello world" } ---- // TESTSETUP //// [source,console] ---- GET my-index/_msearch/template { } { "id": "my-search-template", "params": { "query_string": "hello world", "from": 0, "size": 10 }} { } { "id": "my-other-search-template", "params": { "query_type": "match_all" }} ---- // TEST[s/my-other-search-template/my-search-template/] [[multi-search-template-api-request]] ==== {api-request-title} `GET /_msearch/template` `GET _msearch/template` `POST /_msearch/template` `POST _msearch/template` [[multi-search-template-api-prereqs]] ==== {api-prereq-title} * If the {es} {security-features} are enabled, you must have the `read` <> for the target data stream, index, or alias. For cross-cluster search, see <>. [[multi-search-template-api-path-params]] ==== {api-path-parms-title} ``:: (Optional, string) Comma-separated list of data streams, indices, and aliases to search. Supports wildcards (`*`). To search all data streams and indices, omit this parameter or use `*`. [[multi-search-template-api-query-params]] ==== {api-query-parms-title} `ccs_minimize_roundtrips`:: (Optional, Boolean) If `true`, network round-trips are minimized for cross-cluster search requests. Defaults to `true`. `max_concurrent_searches`:: (Optional, integer) Maximum number of concurrent searches the API can run. Defaults to +max(1, (# of <> * min(<>, 10)))+. `rest_total_hits_as_int`:: (Optional, Boolean) If `true`, the response returns `hits.total` as an integer. If false, it returns `hits.total` as an object. Defaults to `false`. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=search_type] `typed_keys`:: (Optional, Boolean) If `true`, the response prefixes aggregation and suggester names with their respective types. Defaults to `false`. [role="child_attributes"] [[multi-search-template-api-request-body]] ==== {api-request-body-title} The request body must be newline-delimited JSON (NDJSON) in the following format: [source,js] ----
\n \n
\n \n ---- // NOTCONSOLE Each `
` and `` pair represents a search request. The `
` supports the same parameters as the <>'s `
`. The `` supports the same parameters as the <>'s request body. include::multi-search.asciidoc[tag=header-params] ``:: (Request, object) Parameters for the search. + ===== include::search-template-api.asciidoc[tag=body-params] ===== [[multi-search-template-api-response-codes]] ==== {api-response-codes-title} The API returns a `400` status code only if the request itself fails. If one or more searches in the request fail, the API returns a `200` status code with an `error` object for each failed search in the response. [[multi-search-template-api-response-body]] ==== {api-response-body-title} `responses`:: (array of objects) Results for each search, returned in the order submitted. Each object uses the same properties as the <>'s response. + If a search fails, the response includes an `error` object containing an error message. [[multi-search-template-api-curl-requests]] ==== curl requests If a providing text file or text input to `curl`, use the `--data-binary` flag instead of `-d` to preserve newlines. [source,sh] ---- $ cat requests { "index": "my-index" } { "id": "my-search-template", "params": { "query_string": "hello world", "from": 0, "size": 10 }} { "index": "my-other-index" } { "id": "my-other-search-template", "params": { "query_type": "match_all" }} $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch/template --data-binary "@requests"; echo ---- // NOTCONSOLE