Elasticsearch

Elasticsearch is an open source, distributed, RESTful search and analytics engine. Spring Boot offers basic auto-configuration for Elasticsearch clients.

Spring Boot supports several clients:

  • The official low-level REST client

  • The official Java API client

  • The ReactiveElasticsearchClient provided by Spring Data Elasticsearch

Spring Boot provides a dedicated “Starter”, spring-boot-starter-data-elasticsearch.

Connecting to Elasticsearch Using REST clients

Elasticsearch ships two different REST clients that you can use to query a cluster: the low-level client from the org.elasticsearch.client:elasticsearch-rest-client module and the Java API client from the co.elastic.clients:elasticsearch-java module. Additionally, Spring Boot provides support for a reactive client from the org.springframework.data:spring-data-elasticsearch module. By default, the clients will target localhost:9200. You can use spring.elasticsearch.* properties to further tune how the clients are configured, as shown in the following example:

  • Properties

  • YAML

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

Connecting to Elasticsearch Using RestClient

If you have elasticsearch-rest-client on the classpath, Spring Boot will auto-configure and register a RestClient bean. In addition to the properties described previously, to fine-tune the RestClient you can register an arbitrary number of beans that implement RestClientBuilderCustomizer for more advanced customizations. To take full control over the clients' configuration, define a RestClientBuilder bean.

Additionally, if elasticsearch-rest-client-sniffer is on the classpath, a Sniffer is auto-configured to automatically discover nodes from a running Elasticsearch cluster and set them on the RestClient bean. You can further tune how Sniffer is configured, as shown in the following example:

  • Properties

  • YAML

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

Connecting to Elasticsearch Using ElasticsearchClient

If you have co.elastic.clients:elasticsearch-java on the classpath, Spring Boot will auto-configure and register an ElasticsearchClient bean.

The ElasticsearchClient uses a transport that depends upon the previously described RestClient. Therefore, the properties described previously can be used to configure the ElasticsearchClient. Furthermore, you can define a TransportOptions bean to take further control of the behavior of the transport.

Connecting to Elasticsearch using ReactiveElasticsearchClient

Spring Data Elasticsearch ships ReactiveElasticsearchClient for querying Elasticsearch instances in a reactive fashion. If you have Spring Data Elasticsearch and Reactor on the classpath, Spring Boot will auto-configure and register a ReactiveElasticsearchClient.

The ReactiveElasticsearchclient uses a transport that depends upon the previously described RestClient. Therefore, the properties described previously can be used to configure the ReactiveElasticsearchClient. Furthermore, you can define a TransportOptions bean to take further control of the behavior of the transport.

Connecting to Elasticsearch by Using Spring Data

To connect to Elasticsearch, an ElasticsearchClient bean must be defined, auto-configured by Spring Boot or manually provided by the application (see previous sections). With this configuration in place, an ElasticsearchTemplate can be injected like any other Spring bean, as shown in the following example:

  • Java

  • Kotlin

import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

	private final ElasticsearchTemplate template;

	public MyBean(ElasticsearchTemplate template) {
		this.template = template;
	}

	// ...

	public boolean someMethod(String id) {
		return this.template.exists(id, User.class);
	}

}
import org.springframework.stereotype.Component

@Component
class MyBean(private val template: org.springframework.data.elasticsearch.client.erhlc.ElasticsearchRestTemplate ) {

	// ...

	fun someMethod(id: String): Boolean {
		return template.exists(id, User::class.java)
	}

}

In the presence of spring-data-elasticsearch and Reactor, Spring Boot can also auto-configure a ReactiveElasticsearchClient and a ReactiveElasticsearchTemplate as beans. They are the reactive equivalent of the 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 constructed for you automatically based on method names.

In fact, both Spring Data JPA and Spring Data Elasticsearch share the same common infrastructure. You could take the JPA example from earlier and, assuming that City is now an Elasticsearch @Document class rather than a JPA @Entity, it works in the same way.

For complete details of Spring Data Elasticsearch, see the reference documentation.

Spring Boot supports both classic and reactive Elasticsearch repositories, using the ElasticsearchRestTemplate or ReactiveElasticsearchTemplate beans. Most likely those beans are auto-configured by Spring Boot given the required dependencies are present.

If you wish to use your own template for backing the Elasticsearch repositories, you can add your own ElasticsearchRestTemplate or ElasticsearchOperations @Bean, as long as it is named "elasticsearchTemplate". Same applies to ReactiveElasticsearchTemplate and ReactiveElasticsearchOperations, with the bean name "reactiveElasticsearchTemplate".

You can choose to disable the repositories support with the following property:

  • Properties

  • YAML

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