Home > database >  Replacement for searchForStream method in ElasticSearch java api client
Replacement for searchForStream method in ElasticSearch java api client

Time:12-23

I am migrating a java application from elastic search high level client to java api client, as required for Spring Boot 3, as it no longer supports 'org.springframework.data.elasticsearch.client.erhlc.ElasticsearchRestTemplate'...

I'm unable to find a new way to search with a stream! Here is an old code snippet...

NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder() //
    .withQuery(q) //
    .withPageable(PageRequest.of(0, 1000)); //

NativeSearchQuery searchQuery = builder.build();

Stream<X> list = elasticsearchTemplate.searchForStream(searchQuery, X.class).stream();

I'm using Spring Boot 3, Elastic Search 8.5.3 and the Java Api 8.5.3...

Here is the gradle dependency...

implementation group: 'co.elastic.clients', name: 'elasticsearch-java', version: "8.5.3"

CodePudding user response:

It took me a while to find, as I could not find it in the official documentation, but the replacement for deprecated

'org.springframework.data.elasticsearch.client.erhlc.ElasticsearchRestTemplate'

is

'org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchTemplate'

Regarding the "searchForStream", now the searches can be converted into streams...

Also you need to include into gradle/maven the updated dependency...

api group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: "5.0.0"

CodePudding user response:

ElasticsearchOperations.searchForStream(Query query, Class<T> clazz) is available in Spring Data Elasticsearch 5.0 as it was in 4.4. This did not change at all. What has changed is that Spring Data Elasticsearch now uses the new client from Elasticsearch.

What has changed is that you cannot use the NativeSearchQueryBuilder as this was creating native queries for the old client. You will now need to use the NativeQueryBuilder.

As for the solution you suggest in your answer: You cannot just use a reactive implementation as a replacement for the existing imperative one.

And the easiest solution would be to not have a ElasticsearchTemplate injected, but use the interface ElasticsearchOperations instead.

  • Related