Spring-Data-Elasticsearch在后台使用什么Elasticsearch客户? [英] What Elasticsearch client does Spring-Data-Elasticsearch use under the hood?
问题描述
我想在我的项目中使用Spring Data Elasticsearch,并且看到了以下内容:
众所周知,从Elasticsearch 7.0.0开始不推荐使用TransportClient,并且预期在Elasticsearch 8.0中已删除。
我的方法是仅使用Spring Data Elasticsearch进行CRUD操作(类似于ORM),并使用High Level REST Client进行搜索,其余所有操作。
所以我想知道ElasticsearchRepository使用哪个客户端来执行其操作,以及该代码在Elasticsearch 8.0版中是否将不再有效。
使用它仍然是一个不错的决定吗版本3.1.5?
一如既往,这要视情况而定。
关于Elasticsearch:当前版本为6.7.0,TransportClient也将在ES7中可用,尽管已弃用,但仅在ES8中将被删除,因此有很多时间可以使用它-尽管您应该考虑替换它。 p>
关于spring-data-elasticsearch:
- 使用
时ElasticsearchTemplate
,您正在使用TransportClient。 - 使用
ElasticsearchRestTemplate
时,您正在使用RestClient(在3.2.0)。 - 使用默认的
ElasticsearchRepository
时,您正在使用TransportClient。 - 使用扩展到e的自定义存储库时xample
SimpleElasticsearchRepository
如下所示,您正在使用RestClient。
示例配置类:
@SpringBootApplication
@EnableElasticsearchRepositories
公共类SpringdataElasticTestApplication {
public static void main(String [] args){
SpringApplication.run(SpringdataElasticTestApplication.class,args);
}
@Bean
RestHighLevelClient elasticsearchClient(){
最终的ClientConfiguration配置= ClientConfiguration.localhost();
RestHighLevelClient客户端= RestClients.create(configuration).rest();
个回头客户;
}
@Bean
ElasticsearchRestTemplate elasticsearchTemplate(){
return new ElasticsearchRestTemplate(elasticsearchClient());
}
}
示例存储库类别:
公共接口PersonRepository扩展了ElasticsearchRepository< Person,Long> {
}
样本POJO类:
@Document(indexName = person)
public class Person {
@Id
private Long id ;
private String lastName;
private String firstName;
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
}
因此,使用3.1.x时,带有3.2.x版本的TransportClient(当前作为里程碑M2提供)也可以使用RestClient。
请确保您的application.yaml(或.properties)确实可以没有任何 spring.data.elasticsearch.cluster-* 属性,因为这些属性将注入ElasticsearchTemplate(运输客户端)。
您将需要在pom(摘录)中同时设置elasticsearch和spring-data-elasticsearch的正确版本:
< ; properties>
< elasticsearch.version> 6.6.1< /elasticsearch.version>
< / properties>
< dependency>
< groupId> org.springframework.data< / groupId>
< artifactId> spring-data-elasticsearch< / artifactId>
<!-REST客户端需要3.2.0->
< version> 3.2.0.M2< / version>
< / dependency>
<存储库>
< id> Spring-Framework-Milestone< / id>
< name> Spring Framework Milestone< / name>
< url> http://maven.springframework.org/milestone/< / url>
< / repository>
I want to use Spring Data Elasticsearch in my project and I saw this:
The well known TransportClient is deprecated as of Elasticsearch 7.0.0 and is expected to be removed in Elasticsearch 8.0.
My approach is to only use Spring Data Elasticsearch to do CRUD operations (ORM-like), and High Level REST Client for searching and all the rest.
So I want to know which client is the ElasticsearchRepository using to perform its operations, and if the code will no longer be valid in version 8.0 of Elasticsearch.
Is it still a good decision to use version 3.1.5?
As always, it depends.
About Elasticsearch: the current version is 6.7.0, TransportClient will be available in ES7 as well although deprecated but will only be removed in ES8, so there is quite some time to use it - although you should think about replacing it.
About spring-data-elasticsearch:
- when using
ElasticsearchTemplate
, you are using the TransportClient. - when using
ElasticsearchRestTemplate
you are using the RestClient (available in 3.2.0). - when using a default
ElasticsearchRepository
you are using the TransportClient. - when using a custom repository extending for example
SimpleElasticsearchRepository
like shown below you are using the RestClient.
sample configuration class:
@SpringBootApplication
@EnableElasticsearchRepositories
public class SpringdataElasticTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringdataElasticTestApplication.class, args);
}
@Bean
RestHighLevelClient elasticsearchClient() {
final ClientConfiguration configuration = ClientConfiguration.localhost();
RestHighLevelClient client = RestClients.create(configuration).rest();
return client;
}
@Bean
ElasticsearchRestTemplate elasticsearchTemplate() {
return new ElasticsearchRestTemplate(elasticsearchClient());
}
}
sample repository class:
public interface PersonRepository extends ElasticsearchRepository<Person, Long> {
}
sample POJO class:
@Document(indexName = "person")
public class Person {
@Id
private Long id;
private String lastName;
private String firstName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
So when using 3.1.x, you only have the TransportClient, with 3.2.x, currently available as milestone M2, you can use the RestClient as well.
Make sure, that your application.yaml (or .properties) does not have any of the spring.data.elasticsearch.cluster-* properties, as these will inject the ElasticsearchTemplate (Transport Client).
And you will need to both set the right version of elasticsearch and of spring-data-elasticsearch in your pom (excerpt):
<properties>
<elasticsearch.version>6.6.1</elasticsearch.version>
</properties>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<!-- need 3.2.0 for REST client-->
<version>3.2.0.M2</version>
</dependency>
<repository>
<id>Spring-Framework-Milestone</id>
<name>Spring Framework Milestone</name>
<url>http://maven.springframework.org/milestone/</url>
</repository>
这篇关于Spring-Data-Elasticsearch在后台使用什么Elasticsearch客户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!