2013-08-28 19:24:34 -04:00
|
|
|
[[search-multi-search]]
|
2019-07-19 14:35:36 -04:00
|
|
|
=== Multi Search API
|
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
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
[[search-multi-search-api-request]]
|
|
|
|
==== {api-request-title}
|
|
|
|
|
|
|
|
`GET /<index>/_msearch`
|
|
|
|
|
|
|
|
|
|
|
|
[[search-multi-search-api-desc]]
|
|
|
|
==== {api-description-title}
|
|
|
|
|
|
|
|
The multi search AP executes several searches from a single API request.
|
|
|
|
The format of the request is similar to the bulk API format and makes use
|
|
|
|
of the newline delimited JSON (NDJSON) format. The structure is as follows (the
|
|
|
|
structure is specifically optimized to reduce parsing if a specific search ends
|
|
|
|
up redirected to another node):
|
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
|
|
|
|
2017-02-08 11:55:50 -05:00
|
|
|
*NOTE*: 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`.
|
|
|
|
|
2019-09-04 04:14:30 -04:00
|
|
|
[[search-multi-search-api-path-params]]
|
|
|
|
==== {api-path-parms-title}
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=index]
|
|
|
|
|
|
|
|
|
|
|
|
[[search-multi-search-api-request-body]]
|
|
|
|
==== {api-request-body-title}
|
|
|
|
|
|
|
|
`aggregations`::
|
|
|
|
(Optional, object) Specifies aggregations.
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=from]
|
|
|
|
|
|
|
|
`max_concurrent_searches`::
|
|
|
|
(Optional, integer) Specifies the maximum number of concurrent searches the
|
|
|
|
multi search API will execute. This default is based on the number of data
|
|
|
|
nodes and the default search thread pool size.
|
|
|
|
|
|
|
|
`max_concurrent_shard_requests`::
|
|
|
|
(Optional, integer) Specifies the maximum number of concurrent shard requests
|
|
|
|
that each sub search request will execute per node. This parameter should be
|
|
|
|
used to protect a single request from overloading a cluster (for example a
|
|
|
|
default request will hit all indices in a cluster which could cause shard
|
|
|
|
request rejections if the number of shards per node is high). Defaults to `5`.
|
|
|
|
In certain scenarios parallelism isn't achieved through concurrent request
|
|
|
|
such that this protection will result in poor performance. For instance in an
|
|
|
|
environment where only a very low number of concurrent search requests are
|
|
|
|
expected it might help to increase this value to a higher number.
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=preference]
|
|
|
|
|
|
|
|
`query`::
|
|
|
|
(Optional, <<query-dsl,query object>>) Defines the search definition using the
|
|
|
|
<<query-dsl,Query DSL>>.
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=search_type]
|
|
|
|
|
|
|
|
`size`::
|
|
|
|
(Optional, integer) The number of hits to return. Defaults to `10`.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-multi-search-api-response-body]]
|
|
|
|
==== {api-response-body-title}
|
|
|
|
|
|
|
|
`responses`::
|
|
|
|
(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
|
|
|
|
response.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-multi-search-api-example]]
|
|
|
|
==== {api-examples-title}
|
|
|
|
|
|
|
|
The header part includes which index / indices to search on, the `search_type`,
|
|
|
|
`preference`, and `routing`. The body includes the typical search body request
|
|
|
|
(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.
|
|
|
|
|
|
|
|
|
2018-10-19 12:47:34 -04:00
|
|
|
The endpoint allows to also search against an index/indices in the URI itself,
|
|
|
|
in which case it will be used as the default unless explicitly defined otherwise
|
|
|
|
in the header. 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
|
2013-08-28 19:24:34 -04:00
|
|
|
requests that don't define an index, and the last one will be executed
|
2017-02-08 11:05:22 -05:00
|
|
|
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
|
2019-09-04 04:14:30 -04:00
|
|
|
also provides support for templates. Submit them like follows for inline
|
|
|
|
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
|
|
|
{
|
2017-07-12 19:07:28 -04:00
|
|
|
"script": {
|
|
|
|
"lang": "mustache",
|
|
|
|
"source": {
|
|
|
|
"query": {
|
|
|
|
"match": {
|
|
|
|
"message": "{{query_string}}"
|
|
|
|
}
|
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
|
|
|
{
|
2017-07-12 19:07:28 -04:00
|
|
|
"script": {
|
|
|
|
"lang": "mustache",
|
|
|
|
"source": {
|
|
|
|
"query": {
|
|
|
|
"term": {
|
|
|
|
"{{field}}": "{{value}}"
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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
|
|
|
|
information.
|