I tried to use the example in here: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html
on how to use scroll with java in elasticsearch. this is the code:
QueryBuilder qb = termQuery("multi", "test");
SearchResponse scrollResp = client.prepareSearch("test")
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(100).get(); //max of 100 hits will be returned for each scroll
//Scroll until no hits are returned
do {
for (SearchHit hit : scrollResp.getHits().getHits()) {
//Handle the hit...
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
} while(scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.
though for some reasons I have an error which says The method prepareSearch(String) is undefined for the type RestHighLevelClient.
my client variable is indeed RestHighLevelClient but in the tutorial it is what it should be.
ant ideas what is the problem?
RestHighLevelClient works differently than a TransportClient.
Following are the steps you must follow if you wish to use scroll with RestHighLevelClient:
Create a SearchRequest:
SearchRequest request = new SearchRequest("test").scroll(new TimeValue(60000));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(qb);
searchSourceBuilder.sort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
request.source(searchSourceBuilder);
Perform the first search:
SearchResponse scrollResp = client.search(sreq);
here client is the RestHighLevelClient.
For subsequent scroll searches create a SearchScrollRequest and then use it for scroll:
scrollResp = client.searchScroll(new SearchScrollRequest(scrollResponse.getScrollId()).scroll(new TimeValue(60000)));
For more information refer :Search Scroll API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With