Multi-Search-Template API
editMulti-Search-Template API
editThe multiSearchTemplate
API executes multiple search template
requests in a single http request in parallel.
Multi-Search-Template Request
editThe MultiSearchTemplateRequest
is built empty and you add all of the searches that
you wish to execute to it:
String [] searchTerms = {"elasticsearch", "logstash", "kibana"}; MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest(); for (String searchTerm : searchTerms) { SearchTemplateRequest request = new SearchTemplateRequest(); request.setRequest(new SearchRequest("posts")); request.setScriptType(ScriptType.INLINE); request.setScript( "{" + " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + " \"size\" : \"{{size}}\"" + "}"); Map<String, Object> scriptParams = new HashMap<>(); scriptParams.put("field", "title"); scriptParams.put("value", searchTerm); scriptParams.put("size", 5); request.setScriptParams(scriptParams); multiRequest.add(request); }
Create an empty |
|
Create one or more |
|
Add the |
Optional arguments
editThe multiSearchTemplate’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.
Synchronous Execution
editThe multiSearchTemplate
method executes `MultiSearchTemplateRequest`s synchronously:
MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
Asynchronous Execution
editThe multiSearchTemplateAsync
method executes MultiSearchTemplateRequest`s asynchronously,
calling the provided `ActionListener
when the response is ready.
client.msearchTemplateAsync(multiRequest, RequestOptions.DEFAULT, listener);
The parameters are the MultiSearchTemplateRequest
to execute and the ActionListener
to use when
the execution completes
The asynchronous method does not block and returns immediately. Once it is
completed the ActionListener
is called back using the onResponse
method
if the execution successfully completed or using the onFailure
method if
it failed.
A typical listener for MultiSearchTemplateResponse
looks like:
MultiSearchTemplateResponse
editThe MultiSearchTemplateResponse
that is returned by executing the multiSearchTemplate
method contains
a MultiSearchTemplateResponse.Item
for each SearchTemplateRequest
in the
MultiSearchTemplateRequest
. Each MultiSearchTemplateResponse.Item
contains an
exception in getFailure
if the request failed or a
SearchResponse
in getResponse
if
the request succeeded:
for (Item item : multiResponse.getResponses()) { if (item.isFailure()) { String error = item.getFailureMessage(); } else { SearchTemplateResponse searchTemplateResponse = item.getResponse(); SearchResponse searchResponse = searchTemplateResponse.getResponse(); searchResponse.getHits(); } }
An array of responses is returned - one response for each request |
|
Failed search template requests have error messages |
|
Successful requests contain a |