2020-02-07

Spring Elasticsearch auto-configuration

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

Spring Boot supports several clients:
• The official Java "Low Level" and "High Level" REST clients
• 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 and the "High Level" client.
If you have the org.elasticsearch.client:elasticsearch-rest-client dependency on the classpath,
Spring Boot will auto-configure and register a RestClient bean that by default targets
localhost:9200. You can further tune how RestClient is configured, as shown in the following
example:
spring.elasticsearch.rest.uris=https://search.example.com:9200
spring.elasticsearch.rest.read-timeout=10s
spring.elasticsearch.rest.username=user
spring.elasticsearch.rest.password=secret

You can also register an arbitrary number of beans that implement RestClientBuilderCustomizer for
more advanced customizations. To take full control over the registration, define a RestClient bean.
If you have the org.elasticsearch.client:elasticsearch-rest-high-level-client dependency on the
classpath, Spring Boot will auto-configure a RestHighLevelClient, which wraps any existing
RestClient bean, reusing its HTTP configuration.

Connecting to Elasticsearch using Reactive REST clients

Spring Data Elasticsearch ships ReactiveElasticsearchClient for querying Elasticsearch instances in
a reactive fashion. It is built on top of WebFlux’s WebClient, so both spring-boot-starterelasticsearch
and spring-boot-starter-webflux dependencies are useful to enable this support.
By default, Spring Boot will auto-configure and register a ReactiveElasticsearchClient bean that
targets localhost:9200. You can further tune how it is configured, as shown in the following
example:
spring.data.elasticsearch.client.reactive.endpoints=search.example.com:9200
spring.data.elasticsearch.client.reactive.use-ssl=true
spring.data.elasticsearch.client.reactive.socket-timeout=10s
spring.data.elasticsearch.client.reactive.username=user
spring.data.elasticsearch.client.reactive.password=secret

If the configuration properties are not enough and you’d like to fully control the client

configuration, you can register a custom ClientConfiguration bean.

Connecting to Elasticsearch by Using Spring Data

To connect to Elasticsearch, a RestHighLevelClient bean must be defined, auto-configured by Spring
Boot or manually provided by the application (see previous sections). With this configuration in
place, an ElasticsearchRestTemplate can be injected like any other Spring bean, as shown in the
following example:
@Component
public class MyBean {
private final ElasticsearchRestTemplate template;
public MyBean(ElasticsearchRestTemplate template) {
this.template = template;
}
// ...
}
In the presence of spring-data-elasticsearch and the required dependencies for using a WebClient
(typically spring-boot-starter-webflux), 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.


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:
spring.data.elasticsearch.repositories.enabled=false


No comments:

Post a Comment