Elasticsearch is an open source, distributed, search and analytics RESTful engine. Spring Boot provides basic autoconfiguration for Elasticsearch clients.

Spring Boot supports several clients:

  • Official low-level Java clients and high-level REST clients

  • ReactiveElasticsearchClient provided by Spring Data Elasticsearch

Spring Boot provides a special "starter" called spring-boot-starter-data-elasticsearch.

Connecting to Elasticsearch via REST clients

Elasticsearch comes with two different REST clients that can be used to send requests to the cluster: a low-level client from the org.elasticsearch.client:elasticsearch-rest-client module and a high-level client from the org.elasticsearch.client:elasticsearch-high-level-client module. In addition, Spring Boot provides support for a reactive client based on the WebClient from the Spring Framework, from the org.springframework.data:spring-data-elasticsearch module. By default, clients will target localhost:9200. You can use the spring.elasticsearch.* properties to further fine-tune the client configuration, as shown in the following example:

Properties
spring.elasticsearch.uris=https://search.example.com:9200
spring.elasticsearch.socket-timeout=10s
spring.elasticsearch.username=user
spring.elasticsearch.password=secret
Yaml
spring:
  elasticsearch:
    uris: "https://search.example.com:9200"
    socket-timeout: "10s"
    username: "user"
    password: "secret"

Connecting to Elasticsearch via RestClient

If elasticsearch-rest-client is in the classpath, Spring Boot will automatically configure and register the RestClient bean. If elasticsearch-rest-high-level-client is in the classpath, the RestHighLevelClient bean will also be automatically configured. Due to the deprecation of RestHighLevelClient by Elasticsearch, its autoconfiguration has been deprecated and will be removed in a future release. In addition to the properties described earlier, you can register an arbitrary number of beans that implement RestClientBuilderCustomizer for more detailed customization of RestClient and RestHighLevelClient. To gain full control over client configuration, define the RestClientBuilder bean.

Additionally, if elasticsearch-rest-client-sniffer is in the classpath, Sniffer will automatically be configured to automatically discover nodes from a running Elasticsearch cluster and install them into a bin RestClient. You can further customize the Sniffer configuration, as shown in the following example:

Properties
spring.elasticsearch.restclient.sniffer.interval=10m
spring.elasticsearch.restclient.sniffer.delay-after-failure=30s
Yaml
spring:
  elasticsearch:
    restclient:
      sniffer:
        interval: "10m"
        delay-after-failure: "30s"

Connecting to Elasticsearch via ReactiveElasticsearchClient

Spring Data Elasticsearch contains a ReactiveElasticsearchClient for querying Elasticsearch instances in a reactive manner. It is a plugin for the WebClient from WebFlux, so the dependencies spring-boot-starter-elasticsearch and spring-boot-starter-webflux are useful to enable support tools code>.

By default, Spring Boot will automatically configure and register the ReactiveElasticsearchClient. In addition to the properties described previously, the spring.elasticsearch.webclient.* properties can be used to configure reactive programming-specific options, as shown in the following example:

Properties
spring.elasticsearch.webclient.max-in-memory-size=1MB
Yaml
spring:
  elasticsearch:
    webclient:
      max-in-memory-size: "1MB"

If the configuration properties of spring.elasticsearch. and spring.elasticsearch.webclient. are not enough and you want fully manage the client configuration, you can register a custom bean ClientConfiguration.

Connecting to Elasticsearch via Spring Data

To connect to Elasticsearch, a RestHighLevelClient bean must be defined, which is automatically configured by Spring Boot or passed manually by the application (see previous sections). With this configuration, ElasticsearchRestTemplate can be implemented just like any other Spring bean, as shown in the following example:

Java
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
    private final ElasticsearchRestTemplate template;
    public MyBean(ElasticsearchRestTemplate template) {
        this.template = template;
    }
 // ...
 public boolean someMethod(String id) {
        return this.template.exists(id, User.class);
    }
}
Kotlin
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate
import org.springframework.stereotype.Component
@Component
class MyBean(private val template: ElasticsearchRestTemplate) {
 // ...
 fun someMethod(id: String): Boolean {
        return template.exists(id, User::class.java)
    }
}

Given spring-data-elasticsearch and the necessary dependencies to use WebClient (usually spring-boot-starter-webflux), Spring Boot can also automatically configure ReactiveElasticsearchClient and ReactiveElasticsearchTemplate as beans. They are the reactive equivalent of other REST clients.

Spring Data Elasticsearch Repositories

Spring Data includes repository support for Elasticsearch. As with the JPA repositories discussed earlier, the basic principle is that queries are built automatically based on method names.

In fact, both Spring Data JPA and Spring Data Elasticsearch share the same general infrastructure. You can take the JPA example from the previous article and, provided that City is now an Elasticsearch class with the @Document annotation, rather than a JPA object with the @Entity annotation, it will work exactly the same.

Spring Boot supports both classic and reactive Elasticsearch repositories through the ElasticsearchRestTemplate or ReactiveElasticsearchTemplate beans. Most likely, these beans will be automatically configured by Spring Boot if the necessary dependencies are present.

If you want to use your own template to provide support for Elasticsearch repositories, you can add your own ElasticsearchRestTemplate or ElasticsearchOperations via a bean marked with the @Bean if its name is "elasticsearchTemplate". The same applies to ReactiveElasticsearchTemplate and ReactiveElasticsearchOperations, but the bean name must be "reactiveElasticsearchTemplate".

You can disable repository support using the following property:

Properties
spring.data.elasticsearch.repositories.enabled=false
Yaml
spring:
  data:
    elasticsearch:
      repositories:
        enabled: false