2014-08-07 12:24:32 -05:00
[[elasticsearch.misc]]
= Miscellaneous Elasticsearch Operation Support
2021-01-18 23:54:55 +01:00
This chapter covers additional support for Elasticsearch operations that cannot be directly accessed via the repository interface.
It is recommended to add those operations as custom implementation as described in <<repositories.custom-implementations>> .
2014-08-07 12:24:32 -05:00
[[elasticsearch.misc.filter]]
== Filter Builder
Filter Builder improves query speed.
====
[source,java]
----
2019-12-24 09:23:23 +01:00
private ElasticsearchOperations operations;
IndexCoordinates index = IndexCoordinates.of("sample-index");
2014-08-07 12:24:32 -05:00
SearchQuery searchQuery = new NativeSearchQueryBuilder()
2019-06-28 21:40:59 +02:00
.withQuery(matchAllQuery())
.withFilter(boolFilter().must(termFilter("id", documentId)))
.build();
2019-12-24 09:23:23 +01:00
Page<SampleEntity> sampleEntities = operations.searchForPage(searchQuery, SampleEntity.class, index);
2014-08-07 12:24:32 -05:00
----
====
2018-08-31 12:40:51 +03:00
[[elasticsearch.scroll]]
== Using Scroll For Big Result Set
2014-08-07 12:24:32 -05:00
2021-01-18 23:54:55 +01:00
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.
2014-08-07 12:24:32 -05:00
[source,java]
----
2019-12-24 09:23:23 +01:00
IndexCoordinates index = IndexCoordinates.of("sample-index");
2014-08-07 12:24:32 -05:00
SearchQuery searchQuery = new NativeSearchQueryBuilder()
2019-06-28 21:40:59 +02:00
.withQuery(matchAllQuery())
.withFields("message")
.withPageable(PageRequest.of(0, 10))
.build();
2018-08-31 12:40:51 +03:00
2020-03-27 17:19:56 +01:00
SearchHitsIterator<SampleEntity> stream = elasticsearchTemplate.searchForStream(searchQuery, SampleEntity.class, index);
2018-08-31 12:40:51 +03:00
List<SampleEntity> sampleEntities = new ArrayList<>();
2020-03-27 17:19:56 +01:00
while (stream.hasNext()) {
sampleEntities.add(stream.next());
2018-08-31 12:40:51 +03:00
}
2020-03-27 17:19:56 +01:00
stream.close();
2014-08-07 12:24:32 -05:00
----
2018-08-31 12:40:51 +03:00
2020-03-27 17:19:56 +01:00
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:
2018-08-31 12:40:51 +03:00
[source,java]
----
2020-03-27 17:19:56 +01:00
@Autowired ElasticsearchRestTemplate template;
2019-12-24 09:23:23 +01:00
IndexCoordinates index = IndexCoordinates.of("sample-index");
2018-08-31 12:40:51 +03:00
SearchQuery searchQuery = new NativeSearchQueryBuilder()
2019-06-28 21:40:59 +02:00
.withQuery(matchAllQuery())
.withFields("message")
.withPageable(PageRequest.of(0, 10))
.build();
2018-08-31 12:40:51 +03:00
2020-03-27 17:19:56 +01:00
SearchScrollHits<SampleEntity> scroll = template.searchScrollStart(1000, searchQuery, SampleEntity.class, index);
2018-08-31 12:40:51 +03:00
2020-03-27 17:19:56 +01:00
String scrollId = scroll.getScrollId();
2018-08-31 12:40:51 +03:00
List<SampleEntity> sampleEntities = new ArrayList<>();
2020-03-27 17:19:56 +01:00
while (scroll.hasSearchHits()) {
sampleEntities.addAll(scroll.getSearchHits());
scrollId = scroll.getScrollId();
scroll = template.searchScrollContinue(scrollId, 1000, SampleEntity.class);
2018-08-31 12:40:51 +03:00
}
2020-03-27 17:19:56 +01:00
template.searchScrollClear(scrollId);
2018-08-31 12:40:51 +03:00
----
2020-01-23 18:03:37 +01:00
2021-01-18 23:54:55 +01:00
To use the Scroll API with repository methods, the return type must defined as `Stream` in the Elasticsearch Repository.
The implementation of the method will then use the scroll methods from the ElasticsearchTemplate.
2020-04-26 16:40:30 +01:00
[source,java]
----
interface SampleEntityRepository extends Repository<SampleEntity, String> {
Stream<SampleEntity> findBy();
}
----
2020-01-23 18:03:37 +01:00
[[elasticsearch.misc.sorts]]
== Sort options
In addition to the default sort options described <<repositories.paging-and-sorting>> Spring Data Elasticsearch has a `GeoDistanceOrder` class which can be used to have the result of a search operation ordered by geographical distance.
If the class to be retrieved has a `GeoPoint` property named _location_, the following `Sort` would sort the results by distance to the given point:
[source,java]
----
Sort.by(new GeoDistanceOrder("location", new GeoPoint(48.137154, 11.5761247)))
----