| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 | [[java-rest-high-multi-search]]=== Multi-Search APIThe `multiSearch` API executes multiple <<java-rest-high-search,`search`>>requests in a single http request in parallel.[[java-rest-high-multi-search-request]]==== Multi-Search RequestThe `MultiSearchRequest` is built empty and you add all of the searches thatyou wish to execute to it:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-request-basic]--------------------------------------------------<1> Create an empty `MultiSearchRequest`.<2> Create an empty `SearchRequest` and populate it just like youwould for a regular <<java-rest-high-search,`search`>>.<3> Add the `SearchRequest` to the `MultiSearchRequest`.<4> Build a second `SearchRequest` and add it to the `MultiSearchRequest`.===== Optional argumentsThe `SearchRequest`s inside of `MultiSearchRequest` support all of<<java-rest-high-search-request-optional,`search`>>'s optional arguments.For example:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-indices-types]--------------------------------------------------<1> Restricts the request to an index<2> Limits the request to a type[[java-rest-high-multi-search-sync]]==== Synchronous ExecutionThe `multiSearch` method executes `MultiSearchRequest`s synchronously:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-execute]--------------------------------------------------[[java-rest-high-multi-search-async]]==== Asynchronous ExecutionThe `multiSearchAsync` method executes `MultiSearchRequest`s asynchronously,calling the provided `ActionListener` when the response is ready.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SearchDocumentationIT.java[search-execute-async]--------------------------------------------------<1> The `MultiSearchRequest` to execute and the `ActionListener` to use whenthe execution completesThe asynchronous method does not block and returns immediately. Once it iscompleted the `ActionListener` is called back using the `onResponse` methodif the execution successfully completed or using the `onFailure` method ifit failed.A typical listener for `MultiSearchResponse` looks like:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-execute-listener]--------------------------------------------------<1> Called when the execution is successfully completed.<2> Called when the whole `SearchRequest` fails.==== MultiSearchResponseThe `MultiSearchResponse` that is returned by executing the `multiSearch` method containsa `MultiSearchResponse.Item` for each `SearchRequest` in the`MultiSearchRequest`. Each `MultiSearchResponse.Item` contains anexception in `getFailure` if the request failed or a<<java-rest-high-search-response,`SearchResponse`>> in `getResponse` ifthe request succeeded:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-response]--------------------------------------------------<1> The item for the first search.<2> It succeeded so `getFailure` returns null.<3> And there is a <<java-rest-high-search-response,`SearchResponse`>> in`getResponse`.<4> The item for the second search.
 |