Reputation: 361
I was trying to fetch data based on batches. For this have written below piece of code
final Scroll scroll = new Scroll(TimeValue.timeValueMillis(1L));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(boolQuery)
.sort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.DESC).size(10000);
SearchRequest req = new SearchRequest("logs");
req.scroll(scroll);
req.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(req, RequestOptions.DEFAULT);
scrollId = searchResponse.getScrollId();
SearchHit[] resultsHits = searchResponse.getHits().getHits();
logger.info("scrollId:{}",scrollId);
while (resultsHits != null && resultsHits.length > 0) {
SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId);
searchScrollRequest.scroll(scroll);
SearchResponse response = client.scroll(searchScrollRequest,RequestOptions.DEFAULT);
scrollId = response.getScrollId();
resultsHits = searchResponse.getHits().getHits();
logger.info("scrollId:{}",scrollId);
logger.info("resultHits:{}",resultsHits.length);
}
But above code is running in an infinite loop. Don't understand what i am missing.
Upvotes: 0
Views: 536
Reputation: 2993
You are using searchResponse
inside your while
loop instead of using response
from SearchResponse response = client.scroll(searchScrollRequest,RequestOptions.DEFAULT);
that's is why you are running in an infinite loop.
Change to:
SearchResponse response = client.scroll(searchScrollRequest,RequestOptions.DEFAULT);
scrollId = response.getScrollId();
resultsHits = response.getHits().getHits();
Hope this helps
Upvotes: 1