Micronaut数据分页全文搜索查询

Micronaut data pagination full-text search query

我使用的是 Micronaut Data 版本 1.0.2。

给定以下 JPA 实体 class:

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String code;
    private String name;
    private String fullName;
}

我可以使用 PageableRepository 的以下方法创建全文搜索查询:

@Repository
public interface ProductRepository extends PageableRepository<Product, Long> {
    Slice<Product> findByCodeLikeOrNameLikeOrFullNameLike(
        String code, String name, String fullName, Pageable pageable);
}

但是,我无法为 name 属性 添加一个标准。我要实现的相当于下面的SQL:

select * from product
where code like '%search%' 
or (name like '%search%' and name not like '%-%')
or full_name like '%search%'

我测试了以下方法:

Slice<Product> findByCodeLikeOrNameLikeAndNotLikeOrFullNameLike
Slice<Product> findByCodeLikeOrNameLikeAndNotContainsOrFullNameLike
Slice<Product> findByCodeLikeOrNameLikeAndNameNotLikeOrFullNameLike
Slice<Product> findByCodeLikeOrNameLikeAndNameNotContainsOrFullNameLike

知道如何让它发挥作用吗?

先谢谢了。

如果你使用 @Query 那么这应该是这样的:

@Query(value = "select p from Product p where (p.code like :code or p.fullName like :fullName or p.name like :name) and p.name not like :ignoreName") 
Slice<Product> fullSearch(String code, String name, String ignoreName, String fullName, Pageable pageable);

这里你可以省去countQuery因为你用的是Slice<Product>

如果要使用 Page<Product>,则需要提供元素总数。那么你需要提供计数查询。

喜欢:

countQuery = "select count(p.id) from Product p where (p.code like :code or p.fullName like :fullName or p.name like :name) and p.name not like :ignoreName")