ElasticsearchTemplate is the Spring Data Elasticsearch class that runs index, search, update and delete operations against an Elasticsearch cluster. Its role has not changed, but the implementation behind it has: the old transport-client version was deprecated in 4.0 and removed in 5.0, and the name now belongs to a template built on the official Elasticsearch Java client. In new code you should depend on the ElasticsearchOperations interface instead of a concrete template class.
The original answer to this question claimed that ElasticsearchRestTemplate is the permanent replacement. That was true for Spring Data Elasticsearch 4.x, but it is out of date for Spring Boot 3 and later.
A template in Spring Data is a thin, high-level client that maps domain objects to the store. For Elasticsearch it gives you:
save, get, delete and bulk indexing.Query objects, including CriteriaQuery, StringQuery and NativeQuery.IndexOperations: create, delete, put mapping and refresh.@Document and @Field.Repositories such as ElasticsearchRepository are built on top of the same template, so everything you do with a repository ends up in these operations.
The history explains why older tutorials and answers disagree.
| Spring Data Elasticsearch | Template classes | Underlying client |
|---|---|---|
| 3.x and earlier | ElasticsearchTemplate | Transport client |
| 4.x | ElasticsearchRestTemplate (recommended), ElasticsearchTemplate (deprecated) | High Level REST Client |
| 5.x and later (Spring Boot 3.x) | ElasticsearchTemplate (new implementation), ElasticsearchRestTemplate (deprecated) | Elasticsearch Java client |
The transport client was deprecated by Elastic itself and the High Level REST Client was deprecated too. Spring Data Elasticsearch followed by moving to co.elastic.clients, the official Java API client. As part of that move, the name ElasticsearchTemplate was reused for the new implementation. Check the reference documentation of the version you use for exact removal dates of the remaining deprecated classes.
Because the concrete class has changed twice, inject the interface. Your code then survives client changes without edits. Spring Boot auto-configures the right implementation when spring-boot-starter-data-elasticsearch is on the classpath.
The original example called putMapping and save together on every indexing call. Creating the index and mapping is a one-time setup step and should not run for each document. Do it once through IndexOperations:
The entity uses Spring Data Elasticsearch annotations, not the MongoDB ones:
A search takes a Query and returns SearchHits, which wrap each entity together with its score and metadata:
For simple conditions you can use CriteriaQuery, which does not need the Elasticsearch query DSL:
Use NativeQuery when you need aggregations, highlighting or full DSL control. Package names under client.elc belong to the Elasticsearch Java client integration, so they exist only on the 5.x line and later.
Use a repository for plain CRUD and derived queries, and drop down to the operations interface for everything else.
If you need non-blocking access, Spring Data Elasticsearch also offers ReactiveElasticsearchOperations and reactive repositories.
The old transport-client based class was deprecated in 4.0 and removed in 5.0. A new class with the same name exists in 5.x and later, built on the Elasticsearch Java client, and it is the current default.
Only on a 4.x project that still uses the High Level REST Client. In newer versions it is deprecated, so prefer ElasticsearchOperations.
Inject ElasticsearchOperations, switch queries to NativeQuery, and update the Boot starter. The query builders from the old org.elasticsearch.index.query package need to be rewritten with the new client's builders.
Yes. StringQuery takes a JSON query string, which is useful when you copy a query from Kibana.
ElasticsearchTemplate and its siblings provide high-level access to Elasticsearch from Spring. The classes changed with the underlying client, from transport client to REST client to the official Java client, so code against ElasticsearchOperations, create indices once through IndexOperations, and use NativeQuery for current Spring Boot 3 projects.