简体   繁体   中英

QueryDSL and Hibernate Search isNull and isNotNull queries

I have a project in which I'm planning to use querydsl with hibernate search. However I have a constraint which blocks and I'm not sure how to implement. I have a oneToMany relationship between 2 classes shown here below (I'm omitting all the non pertienent fields) :

Contact class

public class Contact{

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

AddressTemporal class

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;

}

I've configured lucene to index null fields using a String constant (" NULL ") so that I can query on empty fields using that value.

My problem is that I need to do a query that will search in the addressHistory collection but filtering only those whose where endDate field is null. Now

 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());

But this doesn't work since the isNotNull() and isNull() operators are not supported for search queries in QueryDSL.

and I cannot do something like :

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

because of type safety constraints.

Finally my question is : Is there a way to perform "isNotNull" queries using QueryDSL and hibernate search on non String fields? or do I have to resort to Lucene query syntax?

Thanks

Ulises

I am a bit confused by your example. Where is SearchQuery coming from? Are you mixing Hibernate ORM Criteria queries with Hibernate Search queries. The latter look something like this:

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

There is no special method in the DSL for searching for null values. You just specify your null token value in the matching clause.

Alternatively you can use the native Lucene query API to build your query.

尝试这个

QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Project.class).get();

queryBuilder.keyword().onField(fieldName).ignoreFieldBridge().matching("NULL").createQuery()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM