猿问

Spring-Data-Elasticsearch 在后台使用什么 Elasticsearch

我想在我的项目中使用 Spring Data Elasticsearch,我看到了这个:

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

我的方法是仅使用 Spring Data Elasticsearch 进行 CRUD 操作(类似 ORM),使用高级 REST 客户端进行搜索和所有其他操作。所以我想知道 ElasticsearchRepository 使用哪个客户端来执行其操作,以及代码是否在 Elasticsearch 8.0 版中不再有效。
使用 3.1.5 版仍然是一个好的决定吗?


慕后森
浏览 109回答 2
2回答

莫回无

一如既往,这取决于。关于 Elasticsearch:当前版本是 6.7.0,TransportClient 也将在 ES7 中可用,虽然已弃用但只会在 ES8 中删除,因此使用它有相当长的时间 - 尽管您应该考虑更换它。关于 spring-data-elasticsearch:使用时ElasticsearchTemplate,您使用的是 TransportClient。使用时,ElasticsearchRestTemplate您使用的是 RestClient(在 3.2.0 中可用)。使用默认值时,ElasticsearchRepository您使用的是 TransportClient。当使用扩展的自定义存储库时,例如SimpleElasticsearchRepository如下所示,您正在使用 RestClient。示例配置类:@SpringBootApplication@EnableElasticsearchRepositoriespublic class SpringdataElasticTestApplication {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(SpringdataElasticTestApplication.class, args);&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; RestHighLevelClient elasticsearchClient() {&nbsp; &nbsp; &nbsp; &nbsp; final ClientConfiguration configuration = ClientConfiguration.localhost();&nbsp; &nbsp; &nbsp; &nbsp; RestHighLevelClient client = RestClients.create(configuration).rest();&nbsp; &nbsp; &nbsp; &nbsp; return client;&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; ElasticsearchRestTemplate elasticsearchTemplate() {&nbsp; &nbsp; &nbsp; &nbsp; return new ElasticsearchRestTemplate(elasticsearchClient());&nbsp; &nbsp; }}示例存储库类:public interface PersonRepository extends ElasticsearchRepository<Person, Long> {}示例 POJO 类:@Document(indexName = "person")public class Person {&nbsp; &nbsp; @Id&nbsp; &nbsp; private Long id;&nbsp; &nbsp; private String lastName;&nbsp; &nbsp; private String firstName;&nbsp; &nbsp; public Long getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(Long id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getLastName() {&nbsp; &nbsp; &nbsp; &nbsp; return lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setLastName(String lastName) {&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public String getFirstName() {&nbsp; &nbsp; &nbsp; &nbsp; return firstName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFirstName(String firstName) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstName = firstName;&nbsp; &nbsp; }}因此,当使用 3.1.x 时,您只有 TransportClient,而 3.2.x 目前可用作里程碑 M2,您也可以使用 RestClient。确保您的 application.yaml(或 .properties)没有任何spring.data.elasticsearch.cluster-*属性,因为这些属性将注入 ElasticsearchTemplate(传输客户端)。您需要在 pom 中设置正确版本的 elasticsearch 和 spring-data-elasticsearch(摘录):<properties>&nbsp; &nbsp; <elasticsearch.version>6.6.1</elasticsearch.version></properties>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.data</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-data-elasticsearch</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <!-- need 3.2.0 for REST client-->&nbsp; &nbsp; &nbsp; &nbsp; <version>3.2.0.M2</version>&nbsp; &nbsp; </dependency><repository>&nbsp; &nbsp; <id>Spring-Framework-Milestone</id>&nbsp; &nbsp; <name>Spring Framework Milestone</name>&nbsp; &nbsp; <url>http://maven.springframework.org/milestone/</url></repository>

梵蒂冈之花

是的,它确实使用了传输客户端
随时随地看视频慕课网APP

相关分类

Java
我要回答