Integrating Elasticsearch with Spring Boot
Fast recall
Quick revision
Core answer
Whether you have hands-on experience integrating Elasticsearch into a Spring Boot service, including mapping configuration, query building, and connection setup.
Using the deprecated HighLevelRestClient in newer Spring Boot versions. Spring Boot 3.x uses the Elasticsearch Java Client backed by the new transport layer.
Discuss how to handle the dual-write pattern -- writing to JPA and indexing to Elasticsearch in the same service method -- and how to recover from sync failures.
Interview-ready guide
Interview answer
Spring Data Elasticsearch provides repository-style integration with Elasticsearch through the Elasticsearch REST client, with support for derived query methods, @Query annotations, and the native client for full query control.
Setup
- Add
spring-boot-starter-data-elasticsearch; configurespring.elasticsearch.uris=http://localhost:9200; the auto-configuration wires theElasticsearchClientandElasticsearchRestTemplate - Annotate your model with
@Document(indexName = "products")and field-level mappings with@Field(type = FieldType.Text)or@Field(type = FieldType.Keyword); Spring Data creates the index with the declared mapping on startup if it doesn't exist
Repository layer
- Extend
ElasticsearchRepository— you getsave(),findById(),deleteById(), and derived query methods for free findByNameAndStatus(String name, String status)generates abool.mustquery withmatchon name andtermon status — Spring Data infers the query from the method name
`ElasticsearchRestTemplate` for complex queries
- When derived methods can't express the query — bool with filter, nested aggregations, script score — use
ElasticsearchRestTemplate.search(NativeSearchQuery, Class) - Build the query with
NativeSearchQueryBuilder:new NativeSearchQueryBuilder().withQuery(QueryBuilders.boolQuery().must(matchQuery("name", name)).filter(termQuery("status", "active"))).build()
Managing index lifecycle
- Use
@Settingto configure the analyzer at the index level — define a custom analyzer with edge n-gram tokenizer for autocomplete queries - Re-indexing: create a new index with the updated mapping, use
_reindexto copy documents, then update the alias; Spring Data'sIndexOperations.reindex()wraps this flow
Integrating Elasticsearch with Spring Boot
Direct Answer
Spring Data Elasticsearch provides repository-style integration with Elasticsearch through the ElasticsearchRepository interface and the ElasticsearchOperations / ElasticsearchClient APIs. You annotate your entity with @Document to define the index, map fields with @Field, and use auto-derived query methods or custom @Query annotations for search. For production, use the Elasticsearch Java Client (8.x) for full query DSL access.
Common Mistakes & Pitfalls
Using the deprecated HighLevelRestClient in newer Spring Boot versions. Spring Boot 3.x uses the Elasticsearch Java Client backed by the new transport layer.
Check your understanding
Answer these without looking back at the article.
- How do you handle a failed Elasticsearch write in a dual-write pattern?
- What is the difference between ElasticsearchRepository and ElasticsearchOperations?
- How do you manage index migrations when the document mapping changes?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.