Implementation of Spring Data Elasticsearch repositories

With the domain models implemented successfully, ElasticsearchRepository for those can be implemented using Spring Data Elasticsearch. The specialty here is that there is no need to implement anything. Just writing an interface that extends from the ElasticsearchRepository interface would be sufficient to expose methods to find one, find all, save, delete, and so on. The following code shows ArticleRepository:

public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
Optional<Article> findByLink(String link);
Page<Article> findByTitleContainingAndBodyContaining(String title,
String body, Pageable pageable);
}

Since there are use cases in the application to show Article with a link to that Article, the findByLink method is introduced in the preceding repository; this will return Optional, which may or may not have a value inside. Furthermore, in order to enable the search use case for all the available articles, the findByTitleContainingAndBodyContaining method is introduced, which will return a Page object that will have the content, number of total elements, number of total pages, and so on. So, that full-text search can be done on the title and body properties of Article.

The following code shows UserRepository:

public interface UserRepository extends ElasticsearchRepository<User, String> {
User findByUsername(String username);
}

In the preceding code, there is a method name findByUsername, where username is a property of the User class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.190.93