Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Elasticsearch client does Spring-Data-Elasticsearch use under the hood?

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?

like image 500
ala Avatar asked Mar 04 '19 10:03

ala


People also ask

Does spring data support Elasticsearch?

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.

What is Elasticsearch in Spring boot?

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>

What is ElasticsearchTemplate?

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.

Why ElasticsearchTemplate is deprecated?

The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7.


Video Answer


2 Answers

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>
like image 176
P.J.Meisch Avatar answered Dec 01 '22 01:12

P.J.Meisch


yes, it does indeed use the transport client

like image 45
Marwan Farah Avatar answered Dec 01 '22 01:12

Marwan Farah