ElasticsearchOperations is the central interface of Spring Data Elasticsearch for working with Elasticsearch directly from Java. It lets you index, read, update, delete and search documents, run bulk operations, and manage indices. It sits below the repositories and gives you full control over queries when derived repository methods are not enough.
This article covers its main methods, the query types you pass to it, and which implementation to use in current versions.
The interface combines two groups of functionality:
save, get, delete, exists, count, multiGet, bulkIndex, search and searchOne.indexOps(...) returns an IndexOperations object to create, delete and inspect indices and mappings.Spring Data repositories such as ElasticsearchRepository use it internally. Inject it yourself when you need custom queries, aggregations, bulk writes or index management.
In Spring Data Elasticsearch 5 and later, the main implementation is ElasticsearchTemplate, which is built on the Elasticsearch Java client. Spring Boot creates it for you from the Elasticsearch connection properties.
The older ElasticsearchRestTemplate was built on the High Level REST Client, which Elasticsearch itself deprecated. Tutorials that use ElasticsearchRestTemplate or NativeSearchQuery target older versions, so use ElasticsearchTemplate and NativeQuery in new code, and always program against the ElasticsearchOperations interface.
Assume an entity annotated with @Document(indexName = "product"):
save returns the saved entity, including a generated id. get returns null when the document does not exist. The index comes from the @Document annotation on the class.
search takes a Query and returns SearchHits<T>, which wraps each entity with its score, sort values and highlights.
StringQuery accepts a JSON query string. Use it when you already have a query from Kibana, and never build it by concatenating user input.
| Query type | Best for |
|---|---|
CriteriaQuery | Simple field conditions without writing JSON |
StringQuery | Reusing an existing JSON query |
NativeQuery | Full access to the query DSL, aggregations and sorting |
Writing many documents one by one is slow. Use bulkIndex instead:
Index management goes through IndexOperations:
refresh() makes recent writes visible to search, which is mainly useful in tests, because Elasticsearch refreshes on a schedule by default.
| Situation | Use |
|---|---|
| Basic CRUD and derived queries | Repository |
| Custom DSL queries, aggregations, highlighting | ElasticsearchOperations |
| Bulk loading and index lifecycle | ElasticsearchOperations |
You can mix both. A repository can declare custom fragments that use ElasticsearchOperations internally.
The repository gives you CRUD and derived queries with little code. ElasticsearchOperations is the lower-level API for custom queries and index management.
Not in new code. It depends on the deprecated High Level REST Client. Use ElasticsearchTemplate through the ElasticsearchOperations interface.
Add aggregations to a NativeQuery, run search, and read them from the SearchHits aggregations container.
Elasticsearch makes writes searchable after a refresh, which by default happens about once per second. Call indexOps.refresh() in tests.
ElasticsearchOperations is the template-style API of Spring Data Elasticsearch for documents and indices. Inject the interface, use NativeQuery, CriteriaQuery or StringQuery for searches, use bulkIndex for large writes, and rely on repositories for simple CRUD.