The spring-boot-starter-data-elasticsearch dependency integrates Elasticsearch with Spring Boot through Spring Data Elasticsearch. It auto-configures the Elasticsearch client and gives you repositories, a mapping model and ElasticsearchOperations, so you can index and search documents without handling HTTP calls and JSON by hand.
Since Spring Boot 3, the starter uses the official Elasticsearch Java client, and the old RestHighLevelClient is no longer used. Use it for full-text search, filtering and aggregations over data you copy from your primary database.
The starter brings in Spring Data Elasticsearch and the Elasticsearch client libraries, and Spring Boot auto-configures:
ElasticsearchClient connected to your cluster.ElasticsearchOperations (implemented by ElasticsearchTemplate) for index and query operations.ElasticsearchRepository.The Spring Data Elasticsearch version must match your Elasticsearch cluster generation. Check the compatibility matrix in the Spring Data Elasticsearch reference before upgrading either side.
Configure the connection in application.properties:
Recent Elasticsearch versions enable security and TLS by default, so a plain http:// URL with no credentials often fails. For local development you can start a single node with Docker and disable security, which you must never do on a shared or public system:
Use a version tag that matches the one your Spring Data release supports.
An entity is mapped to an index with @Document. Field types control how text is analysed:
Text fields are analysed for full-text search, while Keyword fields are stored as a whole and used for exact matches, sorting and aggregations. Spring Data creates the index and mapping on startup when the index does not exist, which is convenient for development. In production, manage mappings through your own migration process.
Extend ElasticsearchRepository and Spring Data derives queries from method names:
Use it like any Spring Data repository:
Elasticsearch is near real time: a saved document becomes searchable after the next index refresh, normally within about a second, so a search immediately after save may miss it.
For scoring, filters and aggregations use ElasticsearchOperations with a native query. This example combines a full-text match with an exact filter:
SearchHits carries the score and highlight data along with each document. Filters do not affect scoring and are cached, so put exact conditions in filter rather than must.
Treat Elasticsearch as a search index and not as the source of truth. Keep the authoritative data in a relational or document database and sync changes into the index, using application events, change data capture or a periodic reindex. Plan for reindexing, because changing a field type requires a new index. Using an index alias lets you switch to the new index without downtime.
No. It is only the client side. You run Elasticsearch separately, locally with Docker or as a managed service.
Spring Boot 3 and Spring Data Elasticsearch 5 use the Elasticsearch Java client (ElasticsearchClient). RestHighLevelClient is deprecated upstream and no longer supported by Spring Data.
Indexing is near real time. The document is visible after the next refresh, so allow a short delay or request a refresh explicitly in tests.
Use repositories for simple lookups and derived queries, and ElasticsearchOperations with native queries for scoring, aggregations and complex filters.
The spring-boot-starter-data-elasticsearch dependency auto-configures the Elasticsearch Java client, repositories and ElasticsearchOperations. Map entities with @Document, use repositories for simple queries and native queries for relevance and aggregations, secure the connection, and keep Elasticsearch as a synchronised search index rather than your primary data store.