0

I'm using spring-boot-starter-data-elasticsearch (2.3.0.RELEASE) which uses spring-data-elasticsearch (4.0.0.RELEASE).

The official documentation shows usage for both ElasticsearchRestTemplate and ElasticsearchRepository. What is the recommended approach to index a document?

ElasticsearchRestTemplate.index() or ElasticsearchRepository.save()

A similar question is here, but it is more than 5 years old.

boredDev
  • 317
  • 3
  • 18
  • The repository uses under the hood the template to do the access to Elasticsearch. Normally in Spring Data you use the repositories and get down to the templates when you need to implement functionality that the repository dosn't offer – P.J.Meisch Jun 03 '20 at 17:41
  • @P.J.Meisch Yes! I saw the repository is also using the same template internally. So then it's ok if I'm using the repository directly since indexing is a basic operation and repository supports it. One question though, in the domain object I'm already specifying the index name then why do I have to specify the index name through IndexCoordinates again when using ElasticsearchRestTemplate.index()? Does the template ignore the Document annotation? I see it still honors Field annotations though. – boredDev Jun 03 '20 at 18:04
  • The template (or better the operations interface) has methods overloads that just take the class and others that take an additional `IndexCoordinates` object. You would use the latter if you want to specify a different index than the one define in the `@Document` annotation. – P.J.Meisch Jun 03 '20 at 18:58
  • @P.J.Meisch, In the `ElasticsearchRestTemplate` class I can see only one index method which requires the `IndexCoordinates`. No similar method in `ElasticsearchOperations` as well, and most methods are marked as deprecated. Couldn't find in the documentation as well: https://docs.spring.io/spring-data/elasticsearch/docs/current/api/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.html Am I missing something? – boredDev Jun 03 '20 at 20:12
  • 1
    You have to look a `DocumentOperations`and `SearchOperations`. There all the methods are defined. `ElasticsearchOperations` derives from these two interfaces. `ElasticsearchRestTemplate` is the concrete implementation, but it derives from `AbstractElasticsearchOperations` where all the logi is omplemented that is independent of the concrete client implementation. So have a `ElasticsearchOperations` injected and use that. – P.J.Meisch Jun 04 '20 at 05:22

1 Answers1

0

I have never seen any recommendations regarding what is better to use, repositories or *Operations bean(template is an implementation), using Spring Data Projects, and, in particular, Spring Data ES.

Spring Data Repositories let you write basic CRUD functionality fast giving the benefit of less boilerplate and faster development though under the hood it still uses some type of *Operations bean(which is implemented by a respective template), but if you try to do something advanced you might just run into the problems.

It's not quite true for Spring Data JPA for instance as this is one of the oldest projects and has rich support for a lot of advanced things to be done using repositories(not all though).
Spring Data ES project, on the other hand, not so much(currently). You might struggle writing some advanced queries or straight won't be able to do that.

It's really a matter of preference. I use both depending on what I do. In your case, I would use a repository to index cause it's just a simple create.

improbable
  • 2,308
  • 2
  • 15
  • 28