# Make sure all async requests have been completed

**URL:** <https://discuss.elastic.co/t/make-sure-all-async-requests-have-been-completed/113346>\
**Category:** Elasticsearch\
**Created:** [December 27, 2017, 6:02pm UTC](https://discuss.elastic.co/t/make-sure-all-async-requests-have-been-completed/113346 "2017-12-27T18:02:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prakharjain17](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@prakharjain17](https://discuss.elastic.co/u/prakharjain17)\
**Post date:** [December 27, 2017, 6:02pm UTC](https://discuss.elastic.co/t/make-sure-all-async-requests-have-been-completed/113346/1 "2017-12-27T18:02:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![prakharjain17](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@prakharjain17](https://discuss.elastic.co/u/prakharjain17)\
**Post date:** [December 29, 2017, 7:08am UTC](https://discuss.elastic.co/t/make-sure-all-async-requests-have-been-completed/113346/2 "2017-12-29T07:08:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 29, 2017, 7:33am UTC](https://discuss.elastic.co/t/make-sure-all-async-requests-have-been-completed/113346/3 "2017-12-29T07:33:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 26, 2018, 7:34am UTC](https://discuss.elastic.co/t/make-sure-all-async-requests-have-been-completed/113346/4 "2018-01-26T07:34:10Z")

</div>

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