简体   繁体   English

替换 ElasticSearch java api 客户端中的 searchForStream 方法

[英]Replacement for searchForStream method in ElasticSearch java api client

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'...我正在根据 Spring Boot 3 的要求将 Java 应用程序从弹性搜索高级客户端迁移到 Java API 客户端,因为它不再支持“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...我正在使用 Spring Boot 3、Elastic Search 8.5.3 和 Java Api 8.5.3...

Here is the gradle dependency...这是gradle依赖...

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

It took me a while to find, as I could not find it in the official documentation, but the replacement for deprecated我花了一段时间才找到,因为我在官方文档中找不到它,但是替换为 deprecated

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

is

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

Regarding the "searchForStream", now the searches can be converted into streams...关于“searchForStream”,现在可以将搜索转换为流...

Also you need to include into gradle/maven the updated dependency...您还需要将更新的依赖项包含到 gradle/maven 中......

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

ElasticsearchOperations.searchForStream(Query query, Class<T> clazz) is available in Spring Data Elasticsearch 5.0 as it was in 4.4. ElasticsearchOperations.searchForStream(Query query, Class<T> clazz)在 Spring Data Elasticsearch 5.0 中可用,就像在 4.4 中一样。 This did not change at all.这根本没有改变。 What has changed is that Spring Data Elasticsearch now uses the new client from Elasticsearch.发生变化的是 Spring Data Elasticsearch 现在使用 Elasticsearch 的新客户端。

What has changed is that you cannot use the NativeSearchQueryBuilder as this was creating native queries for the old client.发生变化的是您不能使用NativeSearchQueryBuilder ,因为这是为旧客户端创建本机查询。 You will now need to use the NativeQueryBuilder .您现在需要使用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.最简单的解决方案是不注入ElasticsearchTemplate ,而是使用ElasticsearchOperations接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM