Make sure all async requests have been completed

Hi,
I am using Java high level rest client to search for hundreds of products stored in elastic search.
I am using term query.
Since, there are hundreds of products that needs to be searched, I have used async request for term query in for loop.
But I need to make sure that all requests have been completed once the loop is exited and a new statement needs to be executed.
I am not sure how to do this.

My code looks like this:

for(ProductField field: fields) {
    String type = field.getType();
    String value = field.getValue();
    searchSourceBuilder.query(QueryBuilders.termQuery(type,value));
    request.source(searchSourceBuilder);
    client.searchAsync(request, new ActionListener<SearchResponse>() {
        @Override
        public void onResponse(SearchResponse searchResponse) {

        }

        @Override
        public void onFailure(Exception e) {

        }
    });
}

After for loop the next statement should be executed only if all async requests have been completed.
How should I do this?

Hi, I would like anyone to reply.
This is a basic question of Async requests.
We are having multiple Async request going in parallel and we want a time when all the request end.
Also, there is no multi search in Java High Level Rest Client.

Multi search is coming in 6.2.0.

For the other questions I believe this is more a Java question about asynchronous calls in general than something specific to elasticsearch.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.