如何将搜索查询从ElasticsearchRepository迁移到ElasticsearchOperations
我的存储库类使用标准方法名称 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要创建一个存储库片段,请在 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 anElasticsearchOperations
into the implementations class.As for the changes in your existing repositories: You just need to add your custom interface to the
implements
clause.