Submit Async Search APIedit


A SubmitAsyncSearchRequest allows to submit an asynchronous search task to the cluster. Required arguments are the SearchSourceBuilder defining the search and the target indices:

SearchSourceBuilder searchSource = new SearchSourceBuilder()
String[] indices = new String[] { "my-index" }; 
SubmitAsyncSearchRequest request
        = new SubmitAsyncSearchRequest(searchSource, indices);

The definition of the search to run

The target indices for the search

Optional argumentsedit

The following arguments can optionally be provided:


The minimum time that the request should wait before returning a partial result (defaults to 1 second).

The expiration time of the request (defaults to 5 days).

Controls whether the results should be stored if the request completed within the provided wait_for_completion time (default: false)

Synchronous Executionedit

AsyncSearchResponse response = client.asyncSearch()
        .submit(request, RequestOptions.DEFAULT); 

Execute the request and get back the response as an AsyncSearchResponse object.

Asynchronous Executionedit

The asynchronous execution of a SubmitAsyncSearchRequest allows to use an ActionListener to be called back when the submit request returns. Note that this is does not concern the execution of the submitted search request, which always executes asynchronously. The listener, however, waits for the submit request itself to come back:

    .submitAsync(request, RequestOptions.DEFAULT, listener); 

The SubmitAsyncSearchRequest 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 AsyncSearchResponse looks like:

ActionListener<AsyncSearchResponse> listener =
    new ActionListener<AsyncSearchResponse>() {
        public void onResponse(AsyncSearchResponse response) {

        public void onFailure(Exception e) {

Called when the execution is successfully completed. The response is provided as an argument

Called in case of failure. The raised exception is provided as an argument


The returned AsyncSearchResponse allows to retrieve information about the executed operation as follows:


The SearchResponse, or null if not available yet

The id of the async search request, null if the response isn’t stored

true when the response contains partial results

true when the search is still running

The time the response was created (millis since epoch)

The time the response expires (millis since epoch)

Get failure reasons or null for no failures