This repo is an example of how to implement an iterator and a parallel-safe spliterator for the SearchAfter1 operation in Elasticsearch.
As scrolling and deep pagination is discouraged2, these iterators provider a more performant way.
To use them, copy and paste the needed classes into your project. You might have to adjust the imports.
You need to specify a sort value as defined by the Elasticsearch documentation!
SearchAfterIterable searchAfterIterable =
new SearchAfterIterable(
searchSourceBuilder,
searchRequest -> {
try {
return client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
},
"mydocuments");
for (SearchHit documentFields : searchAfterIterable) {
System.out.println(documentFields);
}
SearchSourceBuilder searchSourceBuilder =
new SearchSourceBuilder().size(5000).query(QueryBuilders.matchAllQuery()).sort("_id");
SearchAfterSpliterator searchAfterSpliterator =
new SearchAfterSpliterator(
searchSourceBuilder,
searchRequest -> {
try {
return client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
},
"mydocuments");
StreamSupport.stream(searchAfterSpliterator, true).forEach(System.out::println);