Multi-Search-Template APIedit

The multiSearchTemplate API executes multiple search template requests in a single http request in parallel.

Multi-Search-Template Requestedit

The 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 MultiSearchTemplateRequest.

Create one or more SearchTemplateRequest objects and populate them just like you would for a regular search template.

Add the SearchTemplateRequest to the MultiSearchTemplateRequest.

Optional argumentsedit

The 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 Executionedit

The multiSearchTemplate method executes `MultiSearchTemplateRequest`s synchronously:

MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);

Asynchronous Executionedit

The 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:

ActionListener<MultiSearchTemplateResponse> listener = new ActionListener<MultiSearchTemplateResponse>() {
    @Override
    public void onResponse(MultiSearchTemplateResponse response) {
        
    }

    @Override
    public void onFailure(Exception e) {
        
    }
};

Called when the execution is successfully completed.

Called when the whole MultiSearchTemplateRequest fails.

MultiSearchTemplateResponseedit

The 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 SearchResponse in getResponse.