如何将搜索查询从ElasticsearchRepository迁移到ElasticsearchOperations

发布于 2025-01-20 11:06:56 字数 1120 浏览 2 评论 0原文

我的存储库类使用标准方法名称​​ Findbyxxx 使用查询。它还使用 nativeSearchQueryBuilder 具有自定义查询。

由于搜索()方法已从ElasticsearchRepository中删除,因此我将创建相应的类以使用elasticsearchoperations。我的代码就像下面。

    @Repository
    public interface BookRepository extends ElasticsearchRepository<Book, String> {
    
         public Optional<Book> findById(String bookId);

        //default public Page<Book> fetchBooksForUser(String userId, Pageable pageable) {...}
    }
    
    @Service
    public class BookOperation{   //new class
    
        @Autowired
        private ElasticsearchOperations elasticsearchOperations;
    
        public Page<Book> fetchBooksForUser(String userId, Pageable pageable) {..use elasticsearchOperations...}
    }
    
    @Service
    public class BookServiceImpl implements BookService {
    
        @Autowired
        private BookRepository bookRepository;
    
        @Autowired
        private BookOperation bookOperation;
    
        //some methods use bookRepository, some methods use formsPackageOperation
    }

我有20多个存储库课程。我不想重复20次。有更好的做法吗?

My repository class uses the queries by using the standard method name findByxxx. It also has custom query using NativeSearchQueryBuilder.

Since search() methods have been removed from ElasticsearchRepository, I will create corresponding class to use ElasticsearchOperations. My code is like below.

    @Repository
    public interface BookRepository extends ElasticsearchRepository<Book, String> {
    
         public Optional<Book> findById(String bookId);

        //default public Page<Book> fetchBooksForUser(String userId, Pageable pageable) {...}
    }
    
    @Service
    public class BookOperation{   //new class
    
        @Autowired
        private ElasticsearchOperations elasticsearchOperations;
    
        public Page<Book> fetchBooksForUser(String userId, Pageable pageable) {..use elasticsearchOperations...}
    }
    
    @Service
    public class BookServiceImpl implements BookService {
    
        @Autowired
        private BookRepository bookRepository;
    
        @Autowired
        private BookOperation bookOperation;
    
        //some methods use bookRepository, some methods use formsPackageOperation
    }

I have more than 20 Repository classes. I don't want to repeat this 20 times. Is there a better to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2025-01-27 11:06:56

您只需要创建一个存储库片段,请在 https://docs.spring.io/spring-data/elasticsearch/docs/current/referent/referent/html/#repositories.custom-implementations

在接口中,您定义了所需的方法,在Impl文件中,您实现了必要的逻辑 - 您只需将elasticsearchoperations注入实现类。

至于现有存储库的更改:您只需要将自定义接口添加到instentement子句中即可。

You just need to create a repository fragment, check tyhe documentation at https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.custom-implementations.

In the interface you define the method you need and in the Impl file you implement the necessary logic - you can just inject an ElasticsearchOperations into the implementations class.

As for the changes in your existing repositories: You just need to add your custom interface to the implements clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文