sorry - fixed.
i have testet it with different sizes.
our test with different sizes:
(first param = document count, second param = size)
code:
@ParameterizedTest
@CsvSource({
"17, 3",
"10, 2",
"0, 1",
"1, 1",
"13, 2",
"10, 3"
"100001, 50",
"100001, 100",
"100001, 500",
"100001, 1000",
"100001, 5000",
"100001, 20000",
})
void newTestScrollClosing(int givenTestObjects, int chunksize) {
client.createIndexByFilename(indexName);
List<TestObject> testObjectList = IntStream.range(0, givenTestObjects).boxed()
.map(i -> new TestObject(UUID.randomUUID().toString(), i))
.collect(Collectors.toList());
client.insertBulk(testObjectList, WriteRequest.RefreshPolicy.IMMEDIATE);
List<TestObject> results = new ArrayList<>();
for (ElasticChunkResult<TestObject> result = client.newStartIterate(chunksize); !result
.isEmpty(); result = client.newNextChunk(result)) {
results.addAll(result.getObjects());
}
assertThat(results).hasSize(givenTestObjects);
assertThat(results).containsExactlyInAnyOrderElementsOf(testObjectList);
}
results with search_after:
results with _scroll:
Java Code for query and search_after:
public ElasticChunkResult<T> newStartIterate(int viewSize) {
SearchRequest searchRequest = new SearchRequest(indexname);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(viewSize);
searchSourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.ASC));
searchRequest.source(searchSourceBuilder);
SearchResponse response;
try {
response = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException("Fehler beim SearchRequest", e);
}
List<T> hits = convertHits(response);
return new ElasticChunkResult<>(determineLastObject(response, hits), hits, viewSize);
}
public ElasticChunkResult<T> newNextChunk(ElasticChunkResult elasticChunkResult) {
if (elasticChunkResult.isEmpty()) {
return elasticChunkResult;
}
SearchRequest searchRequest = new SearchRequest(indexname);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(elasticChunkResult.getChunkSize());
searchSourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.ASC));
searchSourceBuilder.searchAfter(new Object[] { elasticChunkResult.getLastObject() });
searchRequest.source(searchSourceBuilder);
SearchResponse response;
try {
response = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException("Fehler beim SearchRequest", e);
}
List<T> hits = convertHits(response);
return new ElasticChunkResult<>(determineLastObject(response, hits), hits, elasticChunkResult.getChunkSize());
}
private Object determineLastObject(SearchResponse response, List<T> hits) {
Object lastObject = null;
if (!hits.isEmpty()) {
lastObject = response.getHits().getAt(response.getHits().getHits().length - 1).getId();
}
return lastObject;
}