Multi-Search API
editMulti-Search API
editThe multiSearch
API executes multiple search
requests in a single http request in parallel.
Multi-Search Request
editThe MultiSearchRequest
is built empty and you add all of the searches that
you wish to execute to it:
MultiSearchRequest request = new MultiSearchRequest(); SearchRequest firstSearchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy")); firstSearchRequest.source(searchSourceBuilder); request.add(firstSearchRequest); SearchRequest secondSearchRequest = new SearchRequest(); searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("user", "luca")); secondSearchRequest.source(searchSourceBuilder); request.add(secondSearchRequest);
Create an empty |
|
Create an empty |
|
Add the |
|
Build a second |
Synchronous Execution
editThe multiSearch
method executes `MultiSearchRequest`s synchronously:
MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);
Asynchronous Execution
editThe multiSearchAsync
method executes MultiSearchRequest`s asynchronously,
calling the provided `ActionListener
when the response is ready.
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 MultiSearchResponse
looks like:
MultiSearchResponse
editThe MultiSearchResponse
that is returned by executing the multiSearch
method contains
a MultiSearchResponse.Item
for each SearchRequest
in the
MultiSearchRequest
. Each MultiSearchResponse.Item
contains an
exception in getFailure
if the request failed or a
SearchResponse
in getResponse
if
the request succeeded:
MultiSearchResponse.Item firstResponse = response.getResponses()[0]; assertNull(firstResponse.getFailure()); SearchResponse searchResponse = firstResponse.getResponse(); assertEquals(4, searchResponse.getHits().getTotalHits().value); MultiSearchResponse.Item secondResponse = response.getResponses()[1]; assertNull(secondResponse.getFailure()); searchResponse = secondResponse.getResponse(); assertEquals(1, searchResponse.getHits().getTotalHits().value);
The item for the first search. |
|
It succeeded so |
|
And there is a |
|
The item for the second search. |