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

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


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@EnableElasticsearchRepositoriespublic 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>