Skip to content

Latest commit

 

History

History
165 lines (130 loc) · 6.13 KB

elasticsearch-clients.adoc

File metadata and controls

165 lines (130 loc) · 6.13 KB

Elasticsearch Clients

This chapter illustrates configuration and usage of supported Elasticsearch client implementations.

Spring Data Elasticsearch operates upon an Elasticsearch client that is connected to a single Elasticsearch node or a cluster. Although the Elasticsearch Client can be used to work with the cluster, applications using Spring Data Elasticsearch normally use the higher level abstractions of [elasticsearch.operations] and [elasticsearch.repositories].

High Level REST Client

The Java High Level REST Client is the default client of Elasticsearch, it is configured like shown:

Example 1. High Level REST Client
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()  (1)
            .connectedTo("localhost:9200")
            .build();

        return RestClients.create(clientConfiguration).rest();                         (2)
    }
}

// ...

  @Autowired
  RestHighLevelClient highLevelClient;

  RestClient lowLevelClient = highLevelClient.lowLevelClient();                        (3)

// ...

IndexRequest request = new IndexRequest("spring-data")
  .id(randomID())
  .source(singletonMap("feature", "high-level-rest-client"))
  .setRefreshPolicy(IMMEDIATE);

IndexResponse response = highLevelClient.index(request,RequestOptions.DEFAULT);
  1. Use the builder to provide cluster addresses, set default HttpHeaders or enable SSL.

  2. Create the RestHighLevelClient.

  3. It is also possible to obtain the lowLevelRest() client.

Reactive Client

The ReactiveElasticsearchClient is a non official driver based on WebClient. It uses the request/response objects provided by the Elasticsearch core project. Calls are directly operated on the reactive stack, not wrapping async (thread pool bound) responses into reactive types.

Example 2. Reactive REST Client
@Configuration
public class ReactiveRestClientConfig extends AbstractReactiveElasticsearchConfiguration {

    @Override
    @Bean
    public ReactiveElasticsearchClient reactiveElasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder() (1)
            .connectedTo("localhost:9200") //
            .build();
        return ReactiveRestClients.create(clientConfiguration);

    }
}
// ...

Mono<IndexResponse> response = client.index(request ->

  request.index("spring-data")
    .id(randomID())
    .source(singletonMap("feature", "reactive-client"));
);
  1. Use the builder to provide cluster addresses, set default HttpHeaders or enable SSL.

Note
The ReactiveClient response, especially for search operations, is bound to the from (offset) & size (limit) options of the request.

Client Configuration

Client behaviour can be changed via the ClientConfiguration that allows to set options for SSL, connect and socket timeouts, headers and other parameters.

Example 3. Client Configuration
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("some-header", "on every request")                      (1)

ClientConfiguration clientConfiguration = ClientConfiguration.builder()
  .connectedTo("localhost:9200", "localhost:9291")                      (2)
  .usingSsl()                                                           (3)
  .withProxy("localhost:8888")                                          (4)
  .withPathPrefix("ela")                                                (5)
  .withConnectTimeout(Duration.ofSeconds(5))                            (6)
  .withSocketTimeout(Duration.ofSeconds(3))                             (7)
  .withDefaultHeaders(defaultHeaders)                                   (8)
  .withBasicAuth(username, password)                                    (9)
  .withHeaders(() -> {                                                  (10)
    HttpHeaders headers = new HttpHeaders();
    headers.add("currentTime", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    return headers;
  })
  .withClientConfigurer(                                                (11)
    ReactiveRestClients.WebClientConfigurationCallback.from(webClient -> {
  	  // ...
      return webClient;
  	}))
  .withClientConfigurer(                                                (12)
    RestClients.RestClientConfigurationCallback.from(clientBuilder -> {
  	  // ...
      return clientBuilder;
  	}))
  . // ... other options
  .build();
  1. Define default headers, if they need to be customized

  2. Use the builder to provide cluster addresses, set default HttpHeaders or enable SSL.

  3. Optionally enable SSL.

  4. Optionally set a proxy.

  5. Optionally set a path prefix, mostly used when different clusters a behind some reverse proxy.

  6. Set the connection timeout. Default is 10 sec.

  7. Set the socket timeout. Default is 5 sec.

  8. Optionally set headers.

  9. Add basic authentication.

  10. A Supplier<Header> function can be specified which is called every time before a request is sent to Elasticsearch - here, as an example, the current time is written in a header.

  11. for reactive setup a function configuring the WebClient

  12. for non-reactive setup a function configuring the REST client

Important
Adding a Header supplier as shown in above example allows to inject headers that may change over the time, like authentication JWT tokens. If this is used in the reactive setup, the supplier function must not block!

Client Logging

To see what is actually sent to and received from the server Request / Response logging on the transport level needs to be turned on as outlined in the snippet below.

Enable transport layer logging
<logger name="org.springframework.data.elasticsearch.client.WIRE" level="trace"/>
Note
The above applies to both the RestHighLevelClient and ReactiveElasticsearchClient when obtained via RestClients respectively ReactiveRestClients.