ElasticsearchRestTemplate was the main Spring Data Elasticsearch class for running operations against Elasticsearch over the High Level REST Client: indexing documents, querying, bulk writes and index management. It is deprecated in current Spring Data Elasticsearch versions, which use ElasticsearchTemplate on top of the new Elasticsearch Java API Client instead.
If you maintain older code, you will still meet ElasticsearchRestTemplate often, and the programming model carries over almost unchanged. This article explains what the class did, how to use it, and how to move to the current API.
Spring Data Elasticsearch offers two levels of API. Repositories (ElasticsearchRepository) cover simple CRUD and derived queries. The template covers everything else: custom queries, aggregations, bulk operations, index settings and mappings, and scroll or point-in-time searches.
ElasticsearchRestTemplate implemented the ElasticsearchOperations interface, so application code that depends on that interface stays independent of the client. It converts entities to JSON using the mapping annotations, sends requests through the Elasticsearch client, and converts the response back into SearchHits<T>, which carry the entity plus score and metadata.
The template changed with the underlying client.
| Spring Data Elasticsearch | Client used | Main template |
|---|---|---|
| 4.x | High Level REST Client (RHLC) | ElasticsearchRestTemplate |
| 5.0 and later | Elasticsearch Java API Client | ElasticsearchTemplate |
From Spring Data Elasticsearch 5.0 the High Level REST Client based classes are deprecated, and later major lines have removed them. Check the reference documentation of the version your Spring Boot release manages before choosing a class. New projects should use ElasticsearchOperations and let Spring Boot create the template.
Add spring-boot-starter-data-elasticsearch and configure the connection in application.properties:
Spring Boot auto-configures the client and template, so you inject the interface:
Depending on ElasticsearchOperations rather than a concrete class is what lets you upgrade from the REST-client template to the new one without touching service code.
Mapping annotations describe the index and the field types.
Use Text for full-text search and Keyword for exact matching, sorting and aggregations. Dynamic mapping would guess types for you, but explicit types avoid surprises.
save performs an index operation, creating or replacing the document with that id. Bulk work is much faster than saving one by one:
Searches go through search with a query object and return SearchHits.
This example uses NativeQuery, the builder for the new Java API Client. On the older REST-client template the equivalent class was NativeSearchQueryBuilder with QueryBuilders.matchQuery("name", "keyboard"). For simple searches without the native DSL, CriteriaQuery works with both client generations:
Index administration goes through IndexOperations:
createWithMapping() creates the index and applies settings and mappings from the entity annotations. In production, manage index creation, aliases and reindexing deliberately rather than letting application start-up do it.
ElasticsearchRestTemplate with ElasticsearchOperations.NativeSearchQueryBuilder and QueryBuilders with NativeQuery builders, or switch simple cases to CriteriaQuery.RestHighLevelClient beans.Yes, in Spring Data Elasticsearch 5.0 and later, because the High Level REST Client it relied on is deprecated by Elastic. Use ElasticsearchOperations with the template Spring Boot provides.
Repositories give you CRUD and derived queries from an interface. The template exposes the full feature set, including aggregations, bulk operations and index management.
Repositories can create the index on start-up when createIndex is true on @Document, which is the default. Do not rely on that in production where mappings must be controlled.
Yes. StringQuery accepts a JSON query body, though the typed builders are easier to refactor.
ElasticsearchRestTemplate wrapped the High Level REST Client behind a Spring-style API for indexing, searching and index management. It is deprecated, so code against ElasticsearchOperations, use NativeQuery or CriteriaQuery, and plan the migration to the Java API Client based template.