2013-08-28 19:24:34 -04:00
|
|
|
[[search-multi-search]]
|
2020-06-26 17:32:56 -04:00
|
|
|
=== Multi search API
|
|
|
|
++++
|
|
|
|
<titleabbrev>Multi search</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
Executes several searches with a single API request.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2020-02-24 07:41:53 -05:00
|
|
|
[source,console]
|
|
|
|
--------------------------------------------------
|
|
|
|
GET twitter/_msearch
|
|
|
|
{ }
|
|
|
|
{"query" : {"match" : { "message": "this is a test"}}}
|
|
|
|
{"index": "twitter2"}
|
|
|
|
{"query" : {"match_all" : {}}}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TEST[setup:twitter]
|
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
[[search-multi-search-api-request]]
|
|
|
|
==== {api-request-title}
|
|
|
|
|
2020-06-26 17:32:56 -04:00
|
|
|
`GET /<target>/_msearch`
|
2019-09-04 04:14:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
[[search-multi-search-api-desc]]
|
|
|
|
==== {api-description-title}
|
|
|
|
|
2020-02-24 07:41:53 -05:00
|
|
|
The multi search API executes several searches from a single API request.
|
2020-03-23 21:05:15 -04:00
|
|
|
The format of the request is similar to the bulk API format and makes use
|
2020-02-24 07:41:53 -05:00
|
|
|
of the newline delimited JSON (NDJSON) format.
|
|
|
|
|
|
|
|
The structure is as follows:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
header\n
|
|
|
|
body\n
|
|
|
|
header\n
|
|
|
|
body\n
|
|
|
|
--------------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
// NOTCONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2020-02-24 07:41:53 -05:00
|
|
|
This structure is specifically optimized to reduce parsing if a specific search
|
|
|
|
ends up redirected to another node.
|
|
|
|
|
|
|
|
[IMPORTANT]
|
|
|
|
====
|
|
|
|
The final line of data must end with a newline character `\n`. Each newline
|
|
|
|
character may be preceded by a carriage return `\r`. When sending requests to
|
|
|
|
this endpoint the `Content-Type` header should be set to `application/x-ndjson`.
|
|
|
|
====
|
2017-02-08 11:55:50 -05:00
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
[[search-multi-search-api-path-params]]
|
|
|
|
==== {api-path-parms-title}
|
|
|
|
|
2020-06-26 17:32:56 -04:00
|
|
|
`<target>`::
|
|
|
|
(Optional, string)
|
|
|
|
Comma-separated list of data streams, indices, and index aliases to search.
|
|
|
|
+
|
|
|
|
This list acts as a fallback if a search in the request body does not specify an
|
|
|
|
`index` target.
|
2020-02-24 07:41:53 -05:00
|
|
|
+
|
2020-06-26 17:32:56 -04:00
|
|
|
Wildcard (`*`) expressions are supported. To search all data streams and indices
|
|
|
|
in a cluster, omit this parameter or use `_all` or `*`.
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2020-02-24 07:41:53 -05:00
|
|
|
[[search-multi-search-api-query-params]]
|
|
|
|
==== {api-query-parms-title}
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2020-06-01 19:42:53 -04:00
|
|
|
include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
|
2020-02-24 07:41:53 -05:00
|
|
|
|
|
|
|
`ccs_minimize_roundtrips`::
|
|
|
|
(Optional, boolean)
|
|
|
|
If `true`, network roundtrips between the coordinating node and remote clusters
|
|
|
|
are minimized for {ccs} requests. Defaults to `true`. See
|
|
|
|
<<ccs-network-delays>>.
|
|
|
|
|
2020-06-01 19:42:53 -04:00
|
|
|
include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
|
2020-02-24 07:41:53 -05:00
|
|
|
+
|
|
|
|
Defaults to `open`.
|
|
|
|
|
|
|
|
`ignore_throttled`::
|
|
|
|
(Optional, boolean)
|
2020-04-28 08:31:54 -04:00
|
|
|
If `true`, concrete, expanded or aliased indices are ignored when frozen.
|
2020-02-24 07:41:53 -05:00
|
|
|
Defaults to `false`.
|
|
|
|
|
2020-06-01 19:42:53 -04:00
|
|
|
include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
|
2019-09-04 04:14:30 -04:00
|
|
|
|
|
|
|
`max_concurrent_searches`::
|
2020-02-24 07:41:53 -05:00
|
|
|
(Optional, integer)
|
2020-04-15 10:02:33 -04:00
|
|
|
Maximum number of concurrent searches the multi search API can execute. Defaults
|
|
|
|
to +max(1, (# of <<data-node,data nodes>> * min(<<search-threadpool,search thread pool size>>, 10)))+.
|
2020-02-24 07:41:53 -05:00
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
`max_concurrent_shard_requests`::
|
2020-02-24 07:41:53 -05:00
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, integer)
|
|
|
|
Maximum number of concurrent shard requests that each sub-search request
|
2020-03-23 21:05:15 -04:00
|
|
|
executes per node. Defaults to `5`.
|
2020-02-24 07:41:53 -05:00
|
|
|
|
|
|
|
You can use this parameter to prevent a request from overloading a cluster. For
|
2020-06-26 17:32:56 -04:00
|
|
|
example, a default request hits all data streams and indices in a cluster. This
|
|
|
|
could cause shard request rejections if the number of shards per node is high.
|
2020-02-24 07:41:53 -05:00
|
|
|
|
|
|
|
In certain scenarios, parallelism isn't achieved through concurrent requests. In
|
|
|
|
those cases, a low value in this parameter could result in poor performance.
|
|
|
|
For example, in an environment where a very low number of concurrent search
|
|
|
|
requests are expected, a higher value in this parameter may improve performance.
|
|
|
|
--
|
|
|
|
|
|
|
|
`pre_filter_shard_size`::
|
|
|
|
(Optional, integer)
|
|
|
|
Defines a threshold that enforces a pre-filter roundtrip to prefilter search
|
|
|
|
shards based on query rewriting if the number of shards the search request
|
|
|
|
expands to exceeds the threshold. This filter roundtrip can limit the number of
|
|
|
|
shards significantly if for instance a shard can not match any documents based
|
2020-03-23 21:05:15 -04:00
|
|
|
on its rewrite method i.e., if date filters are mandatory to match but the
|
|
|
|
shard bounds and the query are disjoint.
|
|
|
|
When unspecified, the pre-filter phase is executed if any of these
|
|
|
|
conditions is met:
|
|
|
|
- The request targets more than `128` shards.
|
|
|
|
- The request targets one or more read-only index.
|
|
|
|
- The primary sort of the query targets an indexed field.
|
2020-02-24 07:41:53 -05:00
|
|
|
|
|
|
|
`rest_total_hits_as_int`::
|
|
|
|
(Optional, boolean)
|
|
|
|
If `true`, `hits.total` are returned as an integer in the
|
|
|
|
response. Defaults to `false`, which returns an object.
|
|
|
|
|
|
|
|
`routing`::
|
|
|
|
(Optional, string)
|
|
|
|
Custom <<mapping-routing-field,routing value>> used to route search operations
|
|
|
|
to a specific shard.
|
|
|
|
|
|
|
|
`search_type`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, string)
|
|
|
|
Indicates whether global term and document frequencies should be used when
|
2020-03-23 21:05:15 -04:00
|
|
|
scoring returned documents.
|
2020-02-24 07:41:53 -05:00
|
|
|
|
|
|
|
Options are:
|
|
|
|
|
|
|
|
`query_then_fetch`::
|
|
|
|
(default)
|
|
|
|
Documents are scored using local term and document frequencies for the shard.
|
|
|
|
This is usually faster but less accurate.
|
|
|
|
|
|
|
|
`dfs_query_then_fetch`::
|
|
|
|
Documents are scored using global term and document frequencies across all
|
|
|
|
shards. This is usually slower but more accurate.
|
|
|
|
--
|
2020-03-23 21:05:15 -04:00
|
|
|
|
2020-02-24 07:41:53 -05:00
|
|
|
`typed_keys`::
|
|
|
|
(Optional, boolean)
|
|
|
|
Specifies whether aggregation and suggester names should be prefixed by their
|
|
|
|
respective types in the response.
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2020-02-24 07:41:53 -05:00
|
|
|
[[search-multi-search-api-request-body]]
|
|
|
|
==== {api-request-body-title}
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2020-02-24 07:41:53 -05:00
|
|
|
The request body contains a newline-delimited list of search `<header>` and
|
|
|
|
search `<body>` objects.
|
|
|
|
|
|
|
|
`<header>`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Required, object)
|
|
|
|
Contains parameters used to limit or change the subsequent search body request.
|
|
|
|
|
|
|
|
This object is required for each search body but can be empty (`{}`) or a blank
|
|
|
|
line.
|
|
|
|
--
|
|
|
|
|
|
|
|
`allow_no_indices`:::
|
|
|
|
(Optional, boolean)
|
|
|
|
If `true`, the request does *not* return an error if a wildcard expression or
|
|
|
|
`_all` value retrieves only missing or closed indices.
|
|
|
|
+
|
|
|
|
This parameter also applies to <<indices-aliases,index aliases>> that point to a
|
|
|
|
missing or closed index.
|
|
|
|
|
2020-06-01 19:42:53 -04:00
|
|
|
include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
|
2020-02-24 07:41:53 -05:00
|
|
|
+
|
|
|
|
Defaults to `open`.
|
|
|
|
|
|
|
|
`ignore_unavailable`:::
|
|
|
|
(Optional, boolean) If `true`, documents from missing or closed indices are not
|
|
|
|
included in the response. Defaults to `false`.
|
|
|
|
|
|
|
|
`index`:::
|
|
|
|
(Optional, string or array of strings)
|
2020-06-26 17:32:56 -04:00
|
|
|
Data streams, indices, and index aliases to search. Wildcard (`*`) expressions
|
|
|
|
are supported. You can specify multiple targets as an array.
|
|
|
|
+
|
|
|
|
If this parameter is not specified, the `<target>` request path parameter
|
|
|
|
is used as a fallback.
|
2020-02-24 07:41:53 -05:00
|
|
|
|
|
|
|
`preference`:::
|
|
|
|
(Optional, string)
|
|
|
|
Node or shard used to perform the search. Random by default.
|
|
|
|
|
|
|
|
`request_cache`:::
|
|
|
|
(Optional, boolean)
|
|
|
|
If `true`, the request cache can be used for this search. Defaults to
|
|
|
|
index-level settings. See <<shard-request-cache>>.
|
|
|
|
|
|
|
|
`routing`:::
|
|
|
|
(Optional, string)
|
|
|
|
Custom <<mapping-routing-field,routing value>> used to route search operations
|
|
|
|
to a specific shard.
|
|
|
|
|
|
|
|
`search_type`:::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, string)
|
|
|
|
Indicates whether global term and document frequencies should be used when
|
2020-03-23 21:05:15 -04:00
|
|
|
scoring returned documents.
|
2020-02-24 07:41:53 -05:00
|
|
|
|
|
|
|
Options are:
|
|
|
|
|
|
|
|
`query_then_fetch`::
|
|
|
|
(default)
|
|
|
|
Documents are scored using local term and document frequencies for the shard.
|
|
|
|
This is usually faster but less accurate.
|
|
|
|
|
|
|
|
`dfs_query_then_fetch`::
|
|
|
|
Documents are scored using global term and document frequencies across all
|
|
|
|
shards. This is usually slower but more accurate.
|
|
|
|
--
|
|
|
|
|
|
|
|
`<body>`::
|
|
|
|
(Optional, object)
|
|
|
|
Contains parameters for a search request:
|
|
|
|
|
|
|
|
`aggregations`:::
|
|
|
|
(Optional, <<_structuring_aggregations,aggregation object>>)
|
|
|
|
Aggregations you wish to run during the search. See <<search-aggregations>>.
|
|
|
|
|
|
|
|
`query`:::
|
|
|
|
(Optional, <<query-dsl,query DSL object>>) Query you wish to run during the
|
|
|
|
search. Hits matching this query are returned in the response.
|
|
|
|
|
|
|
|
`from`:::
|
|
|
|
(Optional, integer)
|
|
|
|
Starting offset for returned hits. Defaults to `0`.
|
|
|
|
|
|
|
|
`size`:::
|
|
|
|
(Optional, integer)
|
|
|
|
Number of hits to return. Defaults to `10`.
|
2019-09-04 04:14:30 -04:00
|
|
|
|
|
|
|
[[search-multi-search-api-response-body]]
|
|
|
|
==== {api-response-body-title}
|
|
|
|
|
|
|
|
`responses`::
|
2020-03-23 21:05:15 -04:00
|
|
|
(array) Includes the search response and status code for each search request
|
|
|
|
matching its order in the original multi search request. If there was a
|
|
|
|
complete failure for a specific search request, an object with `error` message
|
|
|
|
and corresponding status code will be returned in place of the actual search
|
2019-09-04 04:14:30 -04:00
|
|
|
response.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-multi-search-api-example]]
|
|
|
|
==== {api-examples-title}
|
|
|
|
|
2020-06-26 17:32:56 -04:00
|
|
|
The header part includes which data streams, indices, and index aliases to
|
|
|
|
search. The header also indicates the `search_type`,
|
2020-03-23 21:05:15 -04:00
|
|
|
`preference`, and `routing`. The body includes the typical search body request
|
2019-09-04 04:14:30 -04:00
|
|
|
(including the `query`, `aggregations`, `from`, `size`, and so on).
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
$ cat requests
|
|
|
|
{"index" : "test"}
|
|
|
|
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
|
2015-01-14 05:19:32 -05:00
|
|
|
{"index" : "test", "search_type" : "dfs_query_then_fetch"}
|
2013-08-28 19:24:34 -04:00
|
|
|
{"query" : {"match_all" : {}}}
|
|
|
|
{}
|
|
|
|
{"query" : {"match_all" : {}}}
|
|
|
|
|
|
|
|
{"query" : {"match_all" : {}}}
|
2015-01-14 05:19:32 -05:00
|
|
|
{"search_type" : "dfs_query_then_fetch"}
|
2013-08-28 19:24:34 -04:00
|
|
|
{"query" : {"match_all" : {}}}
|
2017-02-08 11:05:22 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
// NOTCONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2017-02-08 11:05:22 -05:00
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
$ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch --data-binary "@requests"; echo
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
// NOTCONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Note, the above includes an example of an empty header (can also be just
|
|
|
|
without any content) which is supported as well.
|
|
|
|
|
|
|
|
|
2020-06-26 17:32:56 -04:00
|
|
|
The endpoint also allows you to search against data streams, indices, and index
|
|
|
|
aliases in the request path. In this case, it will be used as the default target
|
|
|
|
unless explicitly specified in the header's `index` parameter. For example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
GET twitter/_msearch
|
2013-08-28 19:24:34 -04:00
|
|
|
{}
|
|
|
|
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
|
|
|
|
{}
|
|
|
|
{"query" : {"match_all" : {}}}
|
2017-02-08 11:05:22 -05:00
|
|
|
{"index" : "twitter2"}
|
2013-08-28 19:24:34 -04:00
|
|
|
{"query" : {"match_all" : {}}}
|
|
|
|
--------------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
// TEST[setup:twitter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2017-02-08 11:05:22 -05:00
|
|
|
The above will execute the search against the `twitter` index for all the
|
2020-06-26 17:32:56 -04:00
|
|
|
requests that don't define an `index` target in the request body. The last
|
|
|
|
search will be executed against the `twitter2` index.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The `search_type` can be set in a similar manner to globally apply to
|
|
|
|
all search requests.
|
2013-11-27 11:33:09 -05:00
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2013-11-27 11:33:09 -05:00
|
|
|
[[msearch-security]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Security
|
2013-11-27 11:33:09 -05:00
|
|
|
|
|
|
|
See <<url-access-control>>
|
2016-09-13 07:19:25 -04:00
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2016-09-13 07:19:25 -04:00
|
|
|
[[template-msearch]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Template support
|
2016-09-13 07:19:25 -04:00
|
|
|
|
|
|
|
Much like described in <<search-template>> for the _search resource, _msearch
|
2020-03-23 21:05:15 -04:00
|
|
|
also provides support for templates. Submit them like follows for inline
|
2019-09-04 04:14:30 -04:00
|
|
|
templates:
|
2016-09-13 07:19:25 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2016-09-13 07:19:25 -04:00
|
|
|
-----------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
GET _msearch/template
|
|
|
|
{"index" : "twitter"}
|
2017-06-09 11:29:25 -04:00
|
|
|
{ "source" : "{ \"query\": { \"match\": { \"message\" : \"{{keywords}}\" } } } }", "params": { "query_type": "match", "keywords": "some message" } }
|
2017-02-08 11:05:22 -05:00
|
|
|
{"index" : "twitter"}
|
2017-06-09 11:29:25 -04:00
|
|
|
{ "source" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
|
2016-09-13 07:19:25 -04:00
|
|
|
-----------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
// TEST[setup:twitter]
|
2016-09-13 07:19:25 -04:00
|
|
|
|
2017-02-08 11:05:22 -05:00
|
|
|
|
|
|
|
You can also create search templates:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2017-02-08 11:05:22 -05:00
|
|
|
------------------------------------------
|
2017-07-12 19:07:28 -04:00
|
|
|
POST /_scripts/my_template_1
|
2017-02-08 11:05:22 -05:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"script": {
|
|
|
|
"lang": "mustache",
|
|
|
|
"source": {
|
|
|
|
"query": {
|
|
|
|
"match": {
|
|
|
|
"message": "{{query_string}}"
|
2017-02-08 11:05:22 -05:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2017-02-08 11:05:22 -05:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2017-02-08 11:05:22 -05:00
|
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
// TEST[setup:twitter]
|
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2017-02-08 11:05:22 -05:00
|
|
|
------------------------------------------
|
2017-07-12 19:07:28 -04:00
|
|
|
POST /_scripts/my_template_2
|
2017-02-08 11:05:22 -05:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"script": {
|
|
|
|
"lang": "mustache",
|
|
|
|
"source": {
|
|
|
|
"query": {
|
|
|
|
"term": {
|
|
|
|
"{{field}}": "{{value}}"
|
2017-02-08 11:05:22 -05:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2017-02-08 11:05:22 -05:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2017-02-08 11:05:22 -05:00
|
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
// TEST[continued]
|
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
You can use search templates in a _msearch:
|
2016-09-13 07:19:25 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2016-09-13 07:19:25 -04:00
|
|
|
-----------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
GET _msearch/template
|
2016-09-13 07:19:25 -04:00
|
|
|
{"index" : "main"}
|
2017-02-08 11:05:22 -05:00
|
|
|
{ "id": "my_template_1", "params": { "query_string": "some message" } }
|
2016-09-13 07:19:25 -04:00
|
|
|
{"index" : "main"}
|
2017-02-08 11:05:22 -05:00
|
|
|
{ "id": "my_template_2", "params": { "field": "user", "value": "test" } }
|
2017-06-09 03:50:43 -04:00
|
|
|
-----------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
// TEST[continued]
|
2019-04-29 08:28:03 -04:00
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2019-04-29 08:28:03 -04:00
|
|
|
[[multi-search-partial-responses]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Partial responses
|
2019-09-04 04:14:30 -04:00
|
|
|
|
2020-03-23 21:05:15 -04:00
|
|
|
To ensure fast responses, the multi search API will respond with partial results
|
|
|
|
if one or more shards fail. See <<shard-failures, Shard failures>> for more
|
2019-09-04 04:14:30 -04:00
|
|
|
information.
|