Skip to content

Commit 21406a5

Browse files
authored
DATAES-766 - Replace CloseableIterator with SearchHitsIterator in stream operations.
Original PR: #412 fix documentation
1 parent fd71109 commit 21406a5

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

Diff for: src/main/asciidoc/reference/elasticsearch-misc.adoc

+17-18
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ Page<SampleEntity> sampleEntities = operations.searchForPage(searchQuery, Sample
2727
[[elasticsearch.scroll]]
2828
== Using Scroll For Big Result Set
2929

30-
Elasticsearch has a scroll API for getting big result set in chunks. `ElasticsearchOperations` has startScroll and continueScroll methods that can be used as below.
30+
Elasticsearch has a scroll API for getting big result set in chunks. This is internally used by Spring Data Elasticsearch to provide the implementations of the `<T> SearchHitsIterator<T> SearchOperations.searchForStream(Query query, Class<T> clazz, IndexCoordinates index)` method.
3131

32-
.Using startScroll and continueScroll
33-
====
3432
[source,java]
3533
----
3634
IndexCoordinates index = IndexCoordinates.of("sample-index");
@@ -43,25 +41,23 @@ SearchQuery searchQuery = new NativeSearchQueryBuilder()
4341
.withPageable(PageRequest.of(0, 10))
4442
.build();
4543
46-
ScrolledPage<SampleEntity> scroll = operations.startScroll(1000, searchQuery, SampleEntity.class, index);
44+
SearchHitsIterator<SampleEntity> stream = elasticsearchTemplate.searchForStream(searchQuery, SampleEntity.class, index);
4745
48-
String scrollId = scroll.getScrollId();
4946
List<SampleEntity> sampleEntities = new ArrayList<>();
50-
while (scroll.hasContent()) {
51-
sampleEntities.addAll(scroll.getContent());
52-
scrollId = scroll.getScrollId();
53-
scroll = operations.continueScroll(scrollId, 1000, SampleEntity.class);
47+
while (stream.hasNext()) {
48+
sampleEntities.add(stream.next());
5449
}
55-
operations.clearScroll(scrollId);
50+
51+
stream.close();
5652
----
57-
====
5853

59-
`ElasticsearchOperations` additionally has the stream method which wraps the scan and scroll operations into a CloseableIterator.
54+
There are no methods in the `SearchOperations` API to access the scroll id, if it should be necessary to access this, the following methods of the `ElasticsearchRestTemplate` can be used:
6055

61-
.Using stream
62-
====
6356
[source,java]
6457
----
58+
59+
@Autowired ElasticsearchRestTemplate template;
60+
6561
IndexCoordinates index = IndexCoordinates.of("sample-index");
6662
6763
SearchQuery searchQuery = new NativeSearchQueryBuilder()
@@ -72,14 +68,17 @@ SearchQuery searchQuery = new NativeSearchQueryBuilder()
7268
.withPageable(PageRequest.of(0, 10))
7369
.build();
7470
75-
CloseableIterator<SampleEntity> stream = elasticsearchTemplate.stream(searchQuery, SampleEntity.class, index);
71+
SearchScrollHits<SampleEntity> scroll = template.searchScrollStart(1000, searchQuery, SampleEntity.class, index);
7672
73+
String scrollId = scroll.getScrollId();
7774
List<SampleEntity> sampleEntities = new ArrayList<>();
78-
while (stream.hasNext()) {
79-
sampleEntities.add(stream.next());
75+
while (scroll.hasSearchHits()) {
76+
sampleEntities.addAll(scroll.getSearchHits());
77+
scrollId = scroll.getScrollId();
78+
scroll = template.searchScrollContinue(scrollId, 1000, SampleEntity.class);
8079
}
80+
template.searchScrollClear(scrollId);
8181
----
82-
====
8382

8483
[[elasticsearch.misc.sorts]]
8584
== Sort options

0 commit comments

Comments
 (0)