To configure an Elasticsearch client in Spring Boot, add spring-boot-starter-data-elasticsearch and set spring.elasticsearch.uris, spring.elasticsearch.username and spring.elasticsearch.password. Spring Boot 3 and later then auto-configures the official Elasticsearch Java client (ElasticsearchClient), plus ElasticsearchTemplate and Spring Data repositories on top of it.
Older tutorials use RestHighLevelClient, spring.data.elasticsearch.cluster-nodes and TransportClient. Those are removed or deprecated, so the setup below replaces them.
For Gradle:
The starter pulls in Spring Data Elasticsearch and the Elasticsearch Java client. The client version must be compatible with your cluster, so check the compatibility matrix in the Spring Data Elasticsearch reference for the Spring Boot version you use. Mixing a client of one major version with a cluster of another is a frequent cause of strange errors.
| Property | Meaning |
|---|---|
spring.elasticsearch.uris | One or more comma-separated node URLs, including the scheme |
spring.elasticsearch.username / password | Basic authentication credentials |
spring.elasticsearch.connection-timeout | Time allowed to open a connection |
spring.elasticsearch.socket-timeout | Time allowed to wait for a response |
spring.elasticsearch.path-prefix | Prefix when the cluster sits behind a proxy path |
Elasticsearch 8 and later enable security and TLS by default. A local Docker node is therefore reached over https with a generated password and certificate. Configure trust through an SSL bundle rather than disabling verification.
The SSL bundle property is the Spring Boot 3.x name. Check the reference documentation for your version if you upgrade, because property names can change between major releases.
Inject the client directly for low-level operations.
The fluent builders give type-safe queries. ElasticsearchOperations (implemented by ElasticsearchTemplate) is the higher-level Spring abstraction that maps entities to documents.
Spring Data derives the query from the method name, so findByName becomes a match query on the name field. Repositories are enabled automatically when the starter is on the classpath.
Use Java configuration when properties are not enough, for example for API keys or custom headers. Extend ElasticsearchConfiguration and build a ClientConfiguration.
When you provide this class, it replaces the property-based setup, so the spring.elasticsearch.* values are no longer used for the client.
Add a quick check at startup, or use the Actuator health endpoint, which includes an Elasticsearch indicator when the client is present.
Connection refused: wrong host or port, or the node is not running.UnknownHostException in Docker: use the service name, not localhost, when the app runs in a container.SSLHandshakeException: the client does not trust the cluster certificate; provide the CA through an SSL bundle.401 Unauthorized: wrong credentials or missing authentication for a secured cluster.NoSuchMethodError or media type errors: client and server versions are not compatible.It was deprecated by Elastic and Spring Data Elasticsearch dropped support for it in version 5, which ships with Spring Boot 3. Use ElasticsearchClient or ElasticsearchOperations instead.
List them in spring.elasticsearch.uris, separated by commas. The client balances requests across the nodes.
Use the ClientConfiguration builder with withDefaultHeaders or the authentication options it offers, and load the key from an environment variable or secret store, never from source code.
Spring Data creates the index for an @Document entity when the repository is first used, if createIndex is true (the default). Disable that and create indexes through scripts or templates if your production user has no create-index permission.
Add spring-boot-starter-data-elasticsearch, set spring.elasticsearch.uris and credentials, and Spring Boot gives you an ElasticsearchClient, ElasticsearchOperations and repositories. Configure TLS properly, keep client and cluster versions compatible, move to Java configuration only when you need more control, and forget the removed RestHighLevelClient.