[[elasticsearch.clients]] = 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 <> and <>. [[elasticsearch.clients.transport]] == Transport Client WARNING: The well known `TransportClient` is deprecated as of Elasticsearch 7 and will be removed in Elasticsearch 8. (https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html[see the Elasticsearch documentation]). Spring Data Elasticsearch will support the `TransportClient` as long as it is available in the used Elasticsearch <> but has deprecated the classes using it since version 4.0. We strongly recommend to use the <> instead of the `TransportClient`. .Transport Client ==== [source,java] ---- @Configuration public class TransportClientConfig extends ElasticsearchConfigurationSupport { @Bean public Client elasticsearchClient() throws UnknownHostException { Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); <1> TransportClient client = new PreBuiltTransportClient(settings); client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); <2> return client; } @Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" }) public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException { return new ElasticsearchTemplate(elasticsearchClient()); } } // ... IndexRequest request = new IndexRequest("spring-data", "elasticsearch", randomID()) .source(someObject) .setRefreshPolicy(IMMEDIATE); IndexResponse response = client.index(request); ---- <1> The `TransportClient` must be configured with the cluster name. <2> The host and port to connect the client to. ==== [[elasticsearch.clients.rest]] == High Level REST Client The Java High Level REST Client is the default client of Elasticsearch, it provides a straight forward replacement for the `TransportClient` as it accepts and returns the very same request/response objects and therefore depends on the Elasticsearch core project. Asynchronous calls are operated upon a client managed thread pool and require a callback to be notified when the request is done. .High Level REST Client ==== [source,java] ---- @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", "elasticsearch", randomID()) .source(singletonMap("feature", "high-level-rest-client")) .setRefreshPolicy(IMMEDIATE); IndexResponse response = highLevelClient.index(request); ---- <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. ==== [[elasticsearch.clients.reactive]] == 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. .Reactive REST Client ==== [source,java] ---- static class Config { @Bean ReactiveElasticsearchClient client() { ClientConfiguration clientConfiguration = ClientConfiguration.builder() <1> .connectedTo("localhost:9200", "localhost:9291") .build(); return ReactiveRestClients.create(clientConfiguration); } } // ... Mono response = client.index(request -> request.index("spring-data") .type("elasticsearch") .id(randomID()) .source(singletonMap("feature", "reactive-client")) .setRefreshPolicy(IMMEDIATE); ); ---- <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. [[elasticsearch.clients.configuration]] == Client Configuration Client behaviour can be changed via the `ClientConfiguration` that allows to set options for SSL, connect and socket timeouts. .Client Configuration ==== [source,java] ---- // optional if Basic Auhtentication is needed HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("es-security-runas-user", "some-user") <1> ClientConfiguration clientConfiguration = ClientConfiguration.builder() .connectedTo("localhost:9200", "localhost:9291") <2> .withProxy("localhost:8888") <3> .withPathPrefix("ela") <4> .withConnectTimeout(Duration.ofSeconds(5)) <5> .withSocketTimeout(Duration.ofSeconds(3)) <6> .useSsl() <7> .withDefaultHeaders(defaultHeaders) <8> .withBasicAuth(username, password) <9> . // ... 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 set a proxy footnote:notreactive[not yet implemented for the reactive client]. <4> Optionally set a path prefix, mostly used when different clusters a behind some reverse proxy. <5> Set the connection timeout. Default is 10 sec. <6> Set the socket timeout. Default is 5 sec. <7> Optionally enable SSL. <8> Optionally set headers. <9> Add basic authentication. ==== [[elasticsearch.clients.logging]] == 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 [source,xml] ---- ---- NOTE: The above applies to both the `RestHighLevelClient` and `ReactiveElasticsearchClient` when obtained via `RestClients` respectively `ReactiveRestClients`, is not available for the `TransportClient`.