# Help requested implementing scroll request in Java Client

**URL:** <https://forum.opensearch.org/t/help-requested-implementing-scroll-request-in-java-client/15807>\
**Category:** OpenSearch Client Libraries\
**Tags:** opensearch-java\
**Created:** [September 7, 2023, 8:10pm UTC](https://forum.opensearch.org/t/help-requested-implementing-scroll-request-in-java-client/15807 "2023-09-07T20:10:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![henryHCL](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@henryHCL](https://forum.opensearch.org/u/henryHCL)\
**Post date:** [September 7, 2023, 8:10pm UTC](https://forum.opensearch.org/t/help-requested-implementing-scroll-request-in-java-client/15807/1 "2023-09-07T20:10:17Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):  
Rest Client 1.3.7  
Java Client 1.0.0

[opensearch-java 1.0.0 javadoc (org.opensearch.client)](https://www.javadoc.io/doc/org.opensearch.client/opensearch-java/1.0.0/org/opensearch/client/opensearch/core/package-summary.html)

**Describe the issue** :  
I’m having trouble understanding the documentation for implementing a scroll request since I will be executing queries that exceed the maximum 10000 responses.

Currently, my code uses SearchResponse as a stopgap measure like so:

List query = new ArrayList\<\>();  
query.add(new MatchQuery.Builder().field(“customerName”).query(FieldValue.of(name)).build()  
.\_toQuery());  
BoolQuery boolQuery = new BoolQuery.Builder().must(query).build();  
SearchResponse response = client.search(s → s.index(“customer-index”).size(10000).query(boolQuery.\_toQuery()),  
Customer.class);

client is an OpensearchClient  
This query is easily expandable by adding more MatchQuerys to the Query List.

This is simply enough, but I am utterly loss on how I might accomplish the same thing with ScrollRequest and response beyond the following:

ScrollRequest request = new ScrollRequest.Builder().build();  
ScrollResponse scrollResponse = client.scroll(request, Customer.class);

This has no query and no size. I know there is the scrollId() method.

Is the following the proper implementation?

String scrollId = response.scrollId();  
ScrollRequest request = new ScrollRequest.Builder().scrollId(scrollId).build();  
ScrollResponse scrollResponse = client.scroll(request, Customer.class);

List\<Hit\> scrollHits = scrollResponse.hits().hits();

while(scrollHits != null && scrollHits.size() \> 0) {  
for (Hit hit : scrollHits) {  
Customer customer = hit.source();  
customerList.add(customer);  
}  
request = new ScrollRequest.Builder().scrollId(scrollId).build();  
scrollResponse = client.scroll(request, Customer.class);  
scrollHits = scrollResponse.hits().hits();  
}

ClearScrollRequest.Builder clearScrollRequestBuilder = new ClearScrollRequest.Builder();  
clearScrollRequestBuilder.scrollId(scrollId);  
client.clearScroll(clearScrollRequestBuilder.build());

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [November 6, 2023, 8:11pm UTC](https://forum.opensearch.org/t/help-requested-implementing-scroll-request-in-java-client/15807/2 "2023-11-06T20:11:08Z")

</div>

Closed on November 6, 2023, 8:11pm UTC

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