java - QueryDSL 和 Hibernate 搜索 isNull 和 isNotNull 查询

标签 java lucene spring-data-jpa hibernate-search querydsl

我有一个项目,计划将 querydsl 与 hibernate 搜索结合使用。 然而,我有一个阻碍,我不知道如何实现。 我在下面显示的两个类之间有一个 oneToMany 关系(我省略了所有不相关的字段):

联系类(class)

public class Contact{

    @OneToMany(mappedBy = "contact")
    @OrderBy("startDate DESC")
    @IndexedEmbedded
    private List<AddressTemporal> addressHistory;
}

AddressTemporal 类

public class AddressTemporal{


    @NotNull
    @ManyToOne
    @ContainedIn
    private Contact contact;

    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO, indexNullAs = Constants.LUCENE_NULL)
    @DateBridge(resolution = Resolution.DAY)
    private Date endDate;

}

我已将 lucene 配置为使用字符串常量(“NULL”)对空字段进行索引,以便我可以使用该值查询空字段。

我的问题是,我需要执行一个查询,在 addressHistory 集合中进行搜索,但仅过滤 endDate 字段为 null 的查询。 现在

 FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
 fullTextEntityManager.createIndexer().startAndWait();
 QContact c = new QContact("contact");

 SearchQuery<Contact> query = new SearchQuery<> fullTextEntityManager.unwrap(FullTextSession.class), c)
 query.where(c.addressHistory.any().endDate.isNotNull());

但这不起作用,因为 QueryDSL 中的搜索查询不支持 isNotNull() 和 isNull() 运算符。

我不能做这样的事情:

query.where(c.addressHistory.any().endDate.eq(Constants.LUCENE_NULL));

由于类型安全限制。

最后我的问题是:有没有办法在非 String 字段上使用 QueryDSL 和 hibernate 搜索执行“isNotNull”查询?或者我是否必须求助于 Lucene 查询语法?

谢谢

尤利西斯

最佳答案

我对你的例子有点困惑。 SearchQuery 来自哪里?您是否将 Hibernate ORM Criteria 查询与 Hibernate Search 查询混合在一起。后者看起来像这样:

QueryBuilder queryBuilder = searchFactory.buildQueryBuilder()
    .forEntity( Contact.class )
    .get();
Query luceneQuery = mythQB.keyword().onField("history").matching("storm").createQuery();

DSL 中没有特殊的方法来搜索空值。您只需在匹配子句中指定空标记值即可。

或者,您可以使用 native Lucene 查询 API 来构建查询。

关于java - QueryDSL 和 Hibernate 搜索 isNull 和 isNotNull 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25891596/

相关文章:

lucene - 使用 Umbraco Examine 按短语中的确切单词搜索

java - Spring数据查询示例

Spring Data JPA 通过嵌套对象 id 查找(嵌套两次)

java - 我可以在类内编辑 hibernate 实体的字段(例如某些域逻辑)还是会导致一些与代理相关的问题?

elasticsearch - 如何在 Elasticsearch 中提高精确匹配而不是多重匹配

java - Lucene 索引 - 单个术语和短语查询

java - 主类java中是静态的,构造函数中是非静态的

java - 每个 uiBinder 有几个 css

java - 求和算法的幂

java - 用java将大量数据插入数据库