Java QueryDSL和Hibernate搜索isNull和isNotNull查询
我有一个项目,我计划将querydsl与hibernate搜索结合使用。 然而,我有一个阻塞的约束,我不知道如何实现。 我在下面显示的两个类之间有一个Java QueryDSL和Hibernate搜索isNull和isNotNull查询,java,lucene,spring-data-jpa,hibernate-search,querydsl,Java,Lucene,Spring Data Jpa,Hibernate Search,Querydsl,我有一个项目,我计划将querydsl与hibernate搜索结合使用。 然而,我有一个阻塞的约束,我不知道如何实现。 我在下面显示的两个类之间有一个oneToMany关系(我省略了所有不相关的字段): 接触类 public class Contact{ @OneToMany(mappedBy = "contact") @OrderBy("startDate DESC") @IndexedEmbedded private List<AddressTempo
oneToMany
关系(我省略了所有不相关的字段):
接触类
public class Contact{
@OneToMany(mappedBy = "contact")
@OrderBy("startDate DESC")
@IndexedEmbedded
private List<AddressTemporal> addressHistory;
}
我已经将lucene配置为使用字符串常量(“null”)索引空字段,以便可以使用该值查询空字段
我的问题是,我需要执行一个查询,该查询将在addressHistory
集合中搜索,但只过滤那些whereendDate
字段为空的查询。
现在
因为类型安全限制
最后,我的问题是:有没有一种方法可以在非String
字段上使用QueryDSL和hibernate搜索执行“isNotNull”查询?还是我必须求助于Lucene查询语法
谢谢
尤利斯我被你的例子弄糊涂了。SearchQuery从何而来?您正在混合使用Hibernate ORM条件查询和Hibernate搜索查询。后者看起来像这样:
QueryBuilder queryBuilder = searchFactory.buildQueryBuilder()
.forEntity( Contact.class )
.get();
Query luceneQuery = mythQB.keyword().onField("history").matching("storm").createQuery();
DSL中没有搜索空值的特殊方法。您只需在matching子句中指定null标记值
或者,您可以使用本机Lucene查询API构建查询。试试这个
QueryBuilder QueryBuilder=fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Project.class.get()代码>
queryBuilder.keyword().onField(fieldName).ignoreFieldBridge().matching(“NULL”).createQuery()
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());
query.where(c.addressHistory.any().endDate.eq(Constants.LUCENE_NULL));
QueryBuilder queryBuilder = searchFactory.buildQueryBuilder()
.forEntity( Contact.class )
.get();
Query luceneQuery = mythQB.keyword().onField("history").matching("storm").createQuery();