user1341300
user1341300

Reputation: 375

Querying Hibernate Search with a logic pattern

I'm integrating Hibernate Search in my project and at the moment it works fine.

Now I want to refine my search in this way: basically I'd like to pass, as a user, a query like term1 AND term2 OR term3 and so on. The number of terms could be different of course.

So my idea is to build a proper search with logical operators to help the users to find what they want to.

Upvotes: 0

Views: 230

Answers (2)

Bilal BBB
Bilal BBB

Reputation: 1164

You can use this stackoverflow answer if you have one entity.

You can use a boolean query like :

Query luceneQuery = b.bool()
    .must(b.keyword().onField("fieldName").matching("term1").createQuery())
    .must(b.keyword().onField("fieldName").matching("term2").createQuery())
    .should(b.keyword().onField("fieldName").matching("term3").createQuery())
    .except(b.keyword().onField("fieldName").matching("term4").createQuery())
    .createQuery();

must : the query must much this term (like AND).

should : the query should this query (like OR).

except : to exclude the document that contains this term (like NOT).

Upvotes: 0

Vickal
Vickal

Reputation: 161

You have to separate your conditions which are using AND and OR by using (). e.g. (term1 AND term2) OR term3

If you are again wanted to use some term the it should be like ((term1 AND term2) OR term3) AND term4 like this.....

Upvotes: 0

Related Questions