# Example of how to use scrollAsync in a loop with High Level Rest Client

**URL:** <https://discuss.elastic.co/t/example-of-how-to-use-scrollasync-in-a-loop-with-high-level-rest-client/214515>\
**Category:** Elasticsearch\
**Created:** [January 10, 2020, 1:43am UTC](https://discuss.elastic.co/t/example-of-how-to-use-scrollasync-in-a-loop-with-high-level-rest-client/214515 "2020-01-10T01:43:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![indrgun](https://avatars.discourse-cdn.com/v4/letter/i/7feea3/32.png) [@indrgun](https://discuss.elastic.co/u/indrgun)\
**Post date:** [January 10, 2020, 1:43am UTC](https://discuss.elastic.co/t/example-of-how-to-use-scrollasync-in-a-loop-with-high-level-rest-client/214515/1 "2020-01-10T01:43:41Z")

</div>

How do you use client.scrollAsync multiple times in a loop like below of Synchronous version of scroll:

while (searchHits != null && searchHits.length \> 0) {  
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);  
scrollRequest.scroll(scroll);  
searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);  
scrollId = searchResponse.getScrollId();  
searchHits = searchResponse.getHits().getHits();  
}

In Asynchronous case, the searchResponse is received inside the scrollListener's onResponse but then I need to get the next scrollId from that, get the searchHits, set a new scrollRequest and do another client.scrollAsync.

Can you suggest an example?  
I have looked into [[No code for] Example of using scrollAsync with the java High Level Rest Client](https://discuss.elastic.co/t/no-code-for-example-of-using-scrollasync-with-the-java-high-level-rest-client/165126)

I wonder why the returned Hits is not collected in that example.  
I looked up other post regarding "scrollAsync" in this ES discussion forum and the .Net use of "scrollAsync" is simpler:

var scrollResponse = client.ScrollAsync(scrollsrequest).Result

One other thing to confirm is that I shouldn't use client.searchAsync if I am going to use client.scrollAsync correct? I am using scrollAsync to speed up retrieval of many documents records that needs to be paginated yes?

Thank you  
-Indra

---

<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:** [February 7, 2020, 1:54am UTC](https://discuss.elastic.co/t/example-of-how-to-use-scrollasync-in-a-loop-with-high-level-rest-client/214515/2 "2020-02-07T01:54:56Z")

</div>

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