2013-08-28 19:24:34 -04:00
|
|
|
[[search-multi-search]]
|
|
|
|
== Multi Search API
|
|
|
|
|
|
|
|
The multi search API allows to execute several search requests within
|
2013-09-03 15:27:49 -04:00
|
|
|
the same API. The endpoint for it is `_msearch`.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The format of the request is similar to the bulk API format, and the
|
|
|
|
structure is as follows (the structure is specifically optimized to
|
|
|
|
reduce parsing if a specific search ends up redirected to another node):
|
|
|
|
|
|
|
|
[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
|
|
|
|
|
|
|
The header part includes which index / indices to search on, optional
|
|
|
|
(mapping) types to search on, the `search_type`, `preference`, and
|
|
|
|
`routing`. The body includes the typical search body request (including
|
2014-08-13 09:43:47 -04:00
|
|
|
the `query`, `aggregations`, `from`, `size`, and so on). Here is an example:
|
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.
|
|
|
|
|
|
|
|
The response returns a `responses` array, which includes the search
|
2016-05-26 01:13:04 -04:00
|
|
|
response and status code for each search request matching its order in
|
|
|
|
the original multi search request. If there was a complete failure for that
|
|
|
|
specific search request, an object with `error` message and corresponding
|
|
|
|
status code will be returned in place of the actual search response.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The endpoint allows to also search against an index/indices and
|
|
|
|
type/types in the URI itself, in which case it will be used as the
|
|
|
|
default unless explicitly defined otherwise in the header. For example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
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
|
|
|
// CONSOLE
|
|
|
|
// 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
|
|
|
|
2016-06-03 08:47:42 -04:00
|
|
|
The msearch's `max_concurrent_searches` request parameter can be used to control
|
|
|
|
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.
|
|
|
|
|
2013-11-27 11:33:09 -05:00
|
|
|
[float]
|
|
|
|
[[msearch-security]]
|
|
|
|
=== Security
|
|
|
|
|
|
|
|
See <<url-access-control>>
|
2016-09-13 07:19:25 -04:00
|
|
|
|
|
|
|
[float]
|
|
|
|
[[template-msearch]]
|
|
|
|
=== Template support
|
|
|
|
|
|
|
|
Much like described in <<search-template>> for the _search resource, _msearch
|
|
|
|
also provides support for templates. Submit them like follows:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
-----------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
GET _msearch/template
|
|
|
|
{"index" : "twitter"}
|
|
|
|
{ "inline" : "{ \"query\": { \"match\": { \"message\" : \"{{keywords}}\" } } } }", "params": { "query_type": "match", "keywords": "some message" } }
|
|
|
|
{"index" : "twitter"}
|
2016-09-13 07:19:25 -04:00
|
|
|
{ "inline" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
|
|
|
|
-----------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[setup:twitter]
|
2016-09-13 07:19:25 -04:00
|
|
|
|
2017-02-08 11:05:22 -05:00
|
|
|
for inline templates.
|
|
|
|
|
|
|
|
You can also create search templates:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
------------------------------------------
|
|
|
|
POST /_search/template/my_template_1
|
|
|
|
{
|
|
|
|
"template": {
|
|
|
|
"query": {
|
|
|
|
"match": {
|
|
|
|
"message": "{{query_string}}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[setup:twitter]
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
------------------------------------------
|
|
|
|
POST /_search/template/my_template_2
|
|
|
|
{
|
|
|
|
"template": {
|
|
|
|
"query": {
|
|
|
|
"term": {
|
|
|
|
"{{field}}": "{{value}}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
and later use them in a _msearch:
|
2016-09-13 07:19:25 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
-----------------------------------------------
|
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" } }
|
2016-09-13 07:19:25 -04:00
|
|
|
----------------------------------------------
|
2017-02-08 11:05:22 -05:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[continued]
|