123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- [[multi-search-template]]
- === Multi search template API
- ++++
- <titleabbrev>Multi search template</titleabbrev>
- ++++
- Runs multiple <<run-multiple-templated-searches,templated searches>> 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 <target>/_msearch/template`
- `GET _msearch/template`
- `POST <target>/_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`
- <<privileges-list-indices,index privilege>> for the target data stream, index,
- or alias. For cross-cluster search, see <<cross-cluster-configuring>>.
- [[multi-search-template-api-path-params]]
- ==== {api-path-parms-title}
- `<target>`::
- (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 <<data-node,data nodes>> *
- min(<<search-threadpool,search thread pool size>>, 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-repo-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]
- ----
- <header>\n
- <body>\n
- <header>\n
- <body>\n
- ----
- // NOTCONSOLE
- Each `<header>` and `<body>` pair represents a search request.
- The `<header>` supports the same parameters as the <<search-multi-search,multi
- search API>>'s `<header>`. The `<body>` supports the same parameters as the
- <<search-search-api-request-body,search template API>>'s request body.
- include::multi-search.asciidoc[tag=header-params]
- `<body>`::
- (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 <<search-search,search API>>'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
|