1

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.

5
  • 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 Commented Jun 3, 2020 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. Commented Jun 3, 2020 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. Commented Jun 3, 2020 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: docs.spring.io/spring-data/elasticsearch/docs/current/api/org/… Am I missing something? Commented Jun 3, 2020 at 20:12
  • 1
    You have to look a DocumentOperationsand 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. Commented Jun 4, 2020 at 5:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.