DATAES-445 - Updated scroll API example.

Original pull request: #218
This commit is contained in:
tsallase 2018-08-31 12:40:51 +03:00 committed by xhaggi
parent 89d0633fd5
commit 0469a3c7be

View File

@ -23,50 +23,55 @@ Page<SampleEntity> sampleEntities =
---- ----
==== ====
[[elasticsearch.scan.and.scroll]] [[elasticsearch.scroll]]
== Using Scan And Scroll For Big Result Set == Using Scroll For Big Result Set
Elasticsearch has scan and scroll feature for getting big result set in chunks. `ElasticsearchTemplate` has scan and scroll methods that can be used as below. Elasticsearch has a scroll API for getting big result set in chunks. `ElasticsearchTemplate` has startScroll and continueScroll methods that can be used as below.
.Using Scan and Scroll .Using startScroll and continueScroll
==== ====
[source,java] [source,java]
---- ----
SearchQuery searchQuery = new NativeSearchQueryBuilder() SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery()) .withQuery(matchAllQuery())
.withIndices("test-index") .withIndices(INDEX_NAME)
.withTypes("test-type") .withTypes(TYPE_NAME)
.withPageable(new PageRequest(0,1)) .withFields("message")
.withPageable(PageRequest.of(0, 10))
.build(); .build();
String scrollId = elasticsearchTemplate.scan(searchQuery,1000,false);
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>(); Page<SampleEntity> scroll = elasticsearchTemplate.startScroll(1000, searchQuery, SampleEntity.class);
boolean hasRecords = true;
while (hasRecords){ String scrollId = ((ScrolledPage) scroll).getScrollId();
Page<SampleEntity> page = elasticsearchTemplate.scroll(scrollId, 5000L , new ResultsMapper<SampleEntity>() List<SampleEntity> sampleEntities = new ArrayList<>();
{ while (scroll.hasContent()) {
@Override sampleEntities.addAll(scroll.getContent());
public Page<SampleEntity> mapResults(SearchResponse response) { scrollId = ((ScrolledPage) scroll).getScrollId();
List<SampleEntity> chunk = new ArrayList<SampleEntity>(); scroll = elasticsearchTemplate.continueScroll(scrollId, 1000, SampleEntity.class);
for(SearchHit searchHit : response.getHits()){ }
if(response.getHits().getHits().length <= 0) { elasticsearchTemplate.clearScroll(scrollId);
return null; ----
} ====
SampleEntity user = new SampleEntity();
user.setId(searchHit.getId()); `ElasticsearchTemplate` additionally has the stream method which wraps the scan and scroll operations into a CloseableIterator.
user.setMessage((String)searchHit.getSource().get("message"));
chunk.add(user); .Using stream
} ====
return new PageImpl<SampleEntity>(chunk); [source,java]
} ----
}); SearchQuery searchQuery = new NativeSearchQueryBuilder()
if(page != null) { .withQuery(matchAllQuery())
sampleEntities.addAll(page.getContent()); .withIndices(INDEX_NAME)
hasRecords = page.hasNextPage(); .withTypes(TYPE_NAME)
} .withFields("message")
else{ .withPageable(PageRequest.of(0, 10))
hasRecords = false; .build();
}
} CloseableIterator<SampleEntity> stream = elasticsearchTemplate.stream(searchQuery, SampleEntity.class);
List<SampleEntity> sampleEntities = new ArrayList<>();
while (stream.hasNext()) {
sampleEntities.add(stream.next());
} }
---- ----
==== ====