Reputation: 1068
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
Upvotes: 3
Views: 6451
Reputation: 509
Try this
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Project.class).get();
queryBuilder.keyword().onField(fieldName).ignoreFieldBridge().matching("NULL").createQuery()
Upvotes: 0
Reputation: 19119
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.
Upvotes: 0