java Elasticsearch Rest Client with Spring Data Elasticsearch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45134811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:31:49  来源:igfitidea点击:

Elasticsearch Rest Client with Spring Data Elasticsearch

javaamazon-web-serviceselasticsearchspring-bootspring-data-elasticsearch

提问by abcdef12

I am in a situation where I am using Spring boot and AWS elasticsearch service. AWS Elasticsearch service which only provides REST interface.

我处于使用 Spring Boot 和 AWS elasticsearch 服务的情况。AWS Elasticsearch 服务,仅提供 REST 接口。

Elasticsearch Rest Client is here.

Elasticsearch Rest Client 来

Simply, Is it possible to use REST client with Spring Data Elasticsearch?

简单地说,是否可以将 REST 客户端与 Spring Data Elasticsearch 一起使用?

In other words, Does Spring Data Elasticsearch works with Elasticsearch Rest client?

换句话说,Spring Data Elasticsearch 是否与 Elasticsearch Rest 客户端配合使用?

Spring Data Elasticsearch is very easy to use and template provides very most functionality that I need. With Elasicsearch Rest client I have to implement all the functionality myself.

Spring Data Elasticsearch 非常易于使用,模板提供了我需要的大部分功能。使用 Elasicsearch Rest 客户端,我必须自己实现所有功能。

回答by Przemek Nowak

[2019 February Update]

【2019年2月更新】

A see now that 3.2.0 M1 Spring Data Elasticsearch supports the HTTP client (https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.M1/reference/html/#reference)

现在看到 3.2.0 M1 Spring Data Elasticsearch 支持 HTTP 客户端(https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.M1/reference/html/#reference

According to the documentation (it could of course change because it's not final version so I will put it here):

根据文档(它当然可以更改,因为它不是最终版本,所以我将把它放在这里):

The well known TransportClient is deprecated as of Elasticsearch 7.0.0 and is expected to be removed in Elasticsearch 8.0.

2.1. High Level REST Client

The Java High Level REST Client provides a straight forward replacement for the TransportClient as it accepts and returns the very same request/response objects and therefore depends on the Elasticsearch core project. Asynchronous calls are operated upon a client managed thread pool and require a callback to be notified when the request is done.

众所周知的 TransportClient 自 Elasticsearch 7.0.0 起已弃用,预计将在 Elasticsearch 8.0 中删除。

2.1. 高级 REST 客户端

Java 高级 REST 客户端为 TransportClient 提供了直接替代,因为它接受并返回完全相同的请求/响应对象,因此依赖于 Elasticsearch 核心项目。异步调用在客户端管理的线程池上运行,并且需要在请求完成时通知回调。

Example 49. High Level REST Client

示例 49. 高级 REST 客户端

static class Config {

  @Bean
  RestHighLevelClient client() {

    ClientConfiguration clientConfiguration = ClientConfiguration.builder() 
      .connectedTo("localhost:9200", "localhost:9201")
      .build();

    return RestClients.create(clientConfiguration).rest(); 
  }
}

// ...

IndexRequest request = new IndexRequest("spring-data", "elasticsearch", randomID())
  .source(singletonMap("feature", "high-level-rest-client"))
  .setRefreshPolicy(IMMEDIATE);

IndexResponse response = client.index(request);


[Original answer]

[原答案]

Currently Spring Data Elasticsearch doesn't support the communication by the REST API. They are using the transport client.

目前 Spring Data Elasticsearch 不支持 REST API 的通信。他们正在使用传输客户端。

There is separate fork of Spring Data Elasticsearch (the guy needed it for AWS the same as you) where the JEST library is used and communication is made by REST:

Spring Data Elasticsearch 有一个单独的分支(他和你一样需要它用于 AWS),其中使用了 JEST 库,并且通过 REST 进行通信:

https://github.com/VanRoy/spring-data-jest

https://github.com/VanRoy/spring-data-jest

You will find the interesting discussion under the following ticked of Spring Data Elasticsearch:

您会在 Spring Data Elasticsearch 的以下勾选下找到有趣的讨论:

https://jira.spring.io/browse/DATAES-220

https://jira.spring.io/browse/DATAES-220

I think the Spring Data Elasticseach will need to migrate to REST on the future according to the statements from Elasticsearch team that they are planning to support only HTTP communication for ES.

根据 Elasticsearch 团队的声明,他们计划仅支持 ES 的 HTTP 通信,我认为 Spring Data Elasticseach 在未来需要迁移到 REST。

Hope it helps.

希望能帮助到你。

回答by JerinDJoy

I think jest client for elasticsearch will serve your purpose. https://github.com/searchbox-io/Jest/tree/master/jest. Jest is a Java HTTP Rest client for ElasticSearch. It has a very good documentation too and have support for all queries in elasticsearch.

我认为 elasticsearch 的 Jest 客户端将满足您的目的。https://github.com/searchbox-io/Jest/tree/master/jest。Jest 是 ElasticSearch 的 Java HTTP Rest 客户端。它也有一个非常好的文档,并且支持 elasticsearch 中的所有查询。

回答by Atanas Rusenov

Can't comment on Przemek Nowak's answer above. If you don't want to wait for Spring Data ES 2.2.x to use the High Level Rest Client, then Spring Data Jestsaves the day.

无法对上面 Przemek Nowak 的回答发表评论。如果您不想等待 Spring Data ES 2.2.x 使用高级 Rest Client,那么Spring Data Jest可以节省一天的时间。

As per their documentation, you first disable the default Spring Data ES autoconfiguration:

根据他们的文档,您首先禁用默认的 Spring Data ES 自动配置:

@SpringBootApplication(exclude = {
    ElasticsearchAutoConfiguration.class, 
    ElasticsearchDataAutoConfiguration.class
})

And that's it - the repositories will now use Jest's implementation. And if you'd like to use the ElasticsearchTemplate, make sure you inject ElasticsearchOperationsinterface instead:

就是这样 - 存储库现在将使用 Jest 的实现。如果您想使用ElasticsearchTemplate,请确保您注入ElasticsearchOperations接口:

private final ElasticsearchOperations esTemplate;