具有 Spring 数据 Elasticsearch 的 Elasticsearch Rest 客户端

Elasticsearch Rest Client with Spring Data Elasticsearch

我正在使用 Spring 引导和 AWS elasticsearch 服务。仅提供REST接口的AWS Elasticsearch服务

Elasticsearch Rest 客户端是 here

简而言之,是否可以将 REST 客户端与 Spring Data Elasticsearch 一起使用?

换句话说,Spring Data Elasticsearch 可以与 Elasticsearch Rest 客户端一起使用吗?

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

[2020年5月更新]

https://spring.io/blog/2020/05/27/what-s-new-in-spring-data-elasticsearch-4-0

如您所见Spring Data Elasticsearch 4.0:

Spring Data Elasticsearch now uses Elasticsearch 7, 7.6.2 in particular. Elasticsearch clusters running on 6.x versions are not supported anymore. The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7.+ Users should switch to ElasticsearchRestTemplate or ReactiveElasticsearchTemplate.

[2019年2月更新]

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

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

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.

示例 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);

[原回答]

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

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

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

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

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

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

希望对您有所帮助。

我认为 elasticsearch 的 jest 客户端可以满足您的需求。 https://github.com/searchbox-io/Jest/tree/master/jest。 Jest 是 ElasticSearch 的 Java HTTP Rest 客户端。它也有很好的文档,支持 elasticsearch 中的所有查询。

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

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

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

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

private final ElasticsearchOperations esTemplate;