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?
The Spring Data Elasticsearch project provides integration with the Elasticsearch search engine. Key functional areas of Spring Data Elasticsearch are a POJO centric model for interacting with a Elastichsearch Documents and easily writing a Repository style data access layer.
Spring Data Elasticsearch provides a Java API for the search engine. In order to use it, we need to add a new dependency to the pom.xml: <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>4.0.0.RELEASE</version> </dependency>
ElasticsearchTemplate, by other hand, is an elasticsearch client for working with your indexes, and it's not typed or related to your domain classes. It's more powerful since you can do many tasks not available to the repository implementation, like deleting an index or making aggregated searchs.
The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7.
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:
ElasticsearchTemplate
, you are using the TransportClient.ElasticsearchRestTemplate
you are using the RestClient (available in 3.2.0).ElasticsearchRepository
you are using the TransportClient.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>
yes, it does indeed use the transport client
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With