2018-11-21 14:45:48 +01:00
[[elasticsearch.clients]]
= Elasticsearch Clients
This chapter illustrates configuration and usage of supported Elasticsearch client implementations.
2019-12-24 09:23:23 +01:00
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>>.
2018-11-21 14:45:48 +01:00
2019-06-28 21:40:59 +02:00
[[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
2019-12-24 09:23:23 +01:00
Elasticsearch <<elasticsearch.versions,version>> but has deprecated the classes using it since version 4.0.
2019-06-28 21:40:59 +02:00
We strongly recommend to use the <<elasticsearch.clients.rest>> instead of the `TransportClient`.
.Transport Client
====
[source,java]
----
2019-12-24 09:23:23 +01:00
@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());
}
2019-06-28 21:40:59 +02:00
}
// ...
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.
====
2018-11-21 14:45:48 +01:00
[[elasticsearch.clients.rest]]
== High Level REST Client
2019-12-24 09:23:23 +01:00
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
2018-11-21 14:45:48 +01:00
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]
----
2019-12-24 09:23:23 +01:00
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
2018-11-21 14:45:48 +01:00
2019-12-24 09:23:23 +01:00
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
2018-11-21 14:45:48 +01:00
2019-12-24 09:23:23 +01:00
final ClientConfiguration clientConfiguration = ClientConfiguration.builder() <1>
.connectedTo("localhost:9200")
.build();
2018-11-21 14:45:48 +01:00
2019-12-24 09:23:23 +01:00
return RestClients.create(clientConfiguration).rest(); <2>
}
2018-11-21 14:45:48 +01:00
}
2019-06-28 21:40:59 +02:00
// ...
@Autowired
RestHighLevelClient highLevelClient;
2019-12-24 09:23:23 +01:00
RestClient lowLevelClient = highLevelClient.lowLevelClient(); <3>
2019-06-28 21:40:59 +02:00
2018-11-21 14:45:48 +01:00
// ...
IndexRequest request = new IndexRequest("spring-data", "elasticsearch", randomID())
.source(singletonMap("feature", "high-level-rest-client"))
.setRefreshPolicy(IMMEDIATE);
2019-06-28 21:40:59 +02:00
IndexResponse response = highLevelClient.index(request);
2018-11-21 14:45:48 +01:00
----
2019-06-28 21:40:59 +02:00
<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.
2018-11-21 14:45:48 +01:00
====
[[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<IndexResponse> response = client.index(request ->
request.index("spring-data")
.type("elasticsearch")
.id(randomID())
.source(singletonMap("feature", "reactive-client"))
.setRefreshPolicy(IMMEDIATE);
);
----
2019-07-16 09:32:33 +02:00
<1> Use the builder to provide cluster addresses, set default `HttpHeaders` or enable SSL.
2018-11-21 14:45:48 +01:00
====
2018-12-05 09:56:46 +01:00
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]
----
2019-06-28 21:40:59 +02:00
// optional if Basic Auhtentication is needed
2019-12-24 09:23:23 +01:00
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("es-security-runas-user", "some-user") <1>
2018-12-05 09:56:46 +01:00
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
2019-06-28 21:40:59 +02:00
.connectedTo("localhost:9200", "localhost:9291") <2>
2019-12-24 09:23:23 +01:00
.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>
2018-12-05 09:56:46 +01:00
. // ... other options
.build();
----
2019-06-28 21:40:59 +02:00
<1> Define default headers, if they need to be customized
<2> Use the builder to provide cluster addresses, set default `HttpHeaders` or enable SSL.
2019-12-24 09:23:23 +01:00
<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.
2018-12-05 09:56:46 +01:00
====
[[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]
----
<logger name="org.springframework.data.elasticsearch.client.WIRE" level="trace"/>
----
2019-06-28 21:40:59 +02:00
NOTE: The above applies to both the `RestHighLevelClient` and `ReactiveElasticsearchClient` when obtained via `RestClients` respectively `ReactiveRestClients`, is not available for the `TransportClient`.