Get Async Search API

edit

Request

edit

A GetAsyncSearchRequest allows to get a running asynchronous search task by its id. Required arguments are the id of a running async search:

GetAsyncSearchRequest request = new GetAsyncSearchRequest(id);

Optional arguments

edit

The following arguments can optionally be provided:

request.setWaitForCompletion(TimeValue.timeValueSeconds(30)); 
request.setKeepAlive(TimeValue.timeValueMinutes(15)); 

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

The expiration time of the request (defaults to none).

Synchronous Execution

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

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

Asynchronous Execution

edit

The asynchronous execution of a GetAsyncSearchRequest allows to use an ActionListener to be called back when the submit request returns:

client.asyncSearch()
    .getAsync(request, RequestOptions.DEFAULT, listener);  

The GetAsyncSearchRequest 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>() {
        @Override
        public void onResponse(AsyncSearchResponse response) {
            
        }

        @Override
        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

Response

edit

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

response.getSearchResponse(); 
response.getId(); 
response.isPartial(); 
response.isRunning(); 
response.getStartTime(); 
response.getExpirationTime(); 
response.getFailure(); 

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