mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-21 11:32:12 +00:00
parent
89d0633fd5
commit
0469a3c7be
@ -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());
|
|
||||||
user.setMessage((String)searchHit.getSource().get("message"));
|
|
||||||
chunk.add(user);
|
|
||||||
}
|
|
||||||
return new PageImpl<SampleEntity>(chunk);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if(page != null) {
|
|
||||||
sampleEntities.addAll(page.getContent());
|
|
||||||
hasRecords = page.hasNextPage();
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
hasRecords = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
----
|
----
|
||||||
====
|
====
|
||||||
|
|
||||||
|
`ElasticsearchTemplate` additionally has the stream method which wraps the scan and scroll operations into a CloseableIterator.
|
||||||
|
|
||||||
|
.Using stream
|
||||||
|
====
|
||||||
|
[source,java]
|
||||||
|
----
|
||||||
|
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||||
|
.withQuery(matchAllQuery())
|
||||||
|
.withIndices(INDEX_NAME)
|
||||||
|
.withTypes(TYPE_NAME)
|
||||||
|
.withFields("message")
|
||||||
|
.withPageable(PageRequest.of(0, 10))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
CloseableIterator<SampleEntity> stream = elasticsearchTemplate.stream(searchQuery, SampleEntity.class);
|
||||||
|
|
||||||
|
List<SampleEntity> sampleEntities = new ArrayList<>();
|
||||||
|
while (stream.hasNext()) {
|
||||||
|
sampleEntities.add(stream.next());
|
||||||
|
}
|
||||||
|
----
|
||||||
|
====
|
Loading…
x
Reference in New Issue
Block a user