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

2

8

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?

Sturgis answered 4/3, 2019 at 10:50 Comment(0)
R
19

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>
Rebut answered 2/4, 2019 at 18:50 Comment(7)
Thank you, exactly what I was looking for.Sturgis
@Ala I made changes to the sample code so that the Repository is just an interface as it should be, and so that the ElasticsearchRestTemplate is automatically injected into the repository. If you have questions about the setup I can make a small sample project and put it on GitHub, just let me know.Rebut
To be able to import a Milestone dependency you will have to add Spring Framework milestone repo : maven.springframework.org/milestoneRalston
You say "when using a custom repository extending for example SimpleElasticsearchRepository like shown below you are using the RestClient." and then extend ElasticsearchRepository which uses the TransportClient.Pretor
@Pretor I struggled with this for a while, seeing the behavior where the ElasticsearchRepository was missing the elasticsearchTemplate bean on application startup. The bean name of the ElasticsearchRestTemplate needs to be ElasticsearchRestTemplate elasticsearchTemplate() {..., once i corrected that then everything else worked the way I expected with the interface extensions.Cagliari
I know that the documentation is horribly outdated and at some places not correct, that's one point I'm just working onRebut
@P.J.Meisch does this strategy work for elasticsearch version 7.x.x as well?Central
O
1

yes, it does indeed use the transport client

Ostracoderm answered 4/3, 2019 at 16:30 Comment(1)
Though seems accepted, but worth adding some more detail to make it more usefulCureton

© 2022 - 2024 — McMap. All rights reserved.