-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: grouping logic: A and (B or C) with search API
PostPosted: Mon Sep 26, 2011 8:22 am 
Beginner
Beginner

Joined: Sun Sep 18, 2011 4:44 pm
Posts: 22
How can I use the Hibernate search API to implement a logic like A and (B or C)?
Somehow BooleanJunction is missing a parenthesis method.

Thanks in advance


Top
 Profile  
 
 Post subject: Re: grouping logic: A and (B or C) with search API
PostPosted: Mon Sep 26, 2011 10:04 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
when using the Boolean queries you can combine several queries, like with

Code:
orderQB.bool()
  .must(
      orderQB.range()
           .onField(“price”).above(100).excludeLimit().createQuery() )
  .must(
      orderQB.keyword(“user.city”).matching(“Paris”)
      .createQuery()
).createQuery();


Now if you need to nest them, each keyword query from this example can be a boolean query in itself.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: grouping logic: A and (B or C) with search API
PostPosted: Mon Sep 26, 2011 10:48 am 
Beginner
Beginner

Joined: Sun Sep 18, 2011 4:44 pm
Posts: 22
Still, I am unable to achieve the desired logic. Maybe it is a Lucene feature and not Hibernate Search specific.
Say, a, b, c are the resp. queries.
Then
qb.bool().must(a).must(qb.bool().should(b).should(c))
includes results for which neither b nor c is true. What I want is that either b or c must be true.

Thanks


Top
 Profile  
 
 Post subject: Re: grouping logic: A and (B or C) with search API
PostPosted: Mon Sep 26, 2011 10:55 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
What I want is that either b or c must be true.

you mean "either or but not both" ?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: grouping logic: A and (B or C) with search API
PostPosted: Tue Sep 27, 2011 5:25 pm 
Beginner
Beginner

Joined: Sun Sep 18, 2011 4:44 pm
Posts: 22
No, I mean either b or c or both.
Just the normal boolean logic.

There seems to be no way to achieve the parenthesis logic with the API. You can only chain queries,
but should(b).should(c) still allows other elements in the result list (I guess with a lower score,
but I want to exclude them completely).

Thanks


Top
 Profile  
 
 Post subject: Re: grouping logic: A and (B or C) with search API
PostPosted: Tue Sep 27, 2011 5:49 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Actually if you write something like
Quote:
should(b).should(c)

it's going to contain exclusively documents matching b or c or both, and nothing else.

If you see different values being returned you might have a problem with the chosen Analyzer.
Can you provide a test ?

You could also use Projections on FullTextQuery.EXPLANATION to have the query engine explain why it's returning each result.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: grouping logic: A and (B or C) with search API
PostPosted: Wed Sep 28, 2011 9:47 am 
Beginner
Beginner

Joined: Sun Sep 18, 2011 4:44 pm
Posts: 22
The query I use has
bj = bj.should(qb.keyword().wildcard().onField("field1")
.matching(estabName + "*").createQuery());
bj = bj.should(qb.keyword().onField("field1")
.matching(estabName).createQuery());
The field is annotated like
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)

(I omit other must junctions.)

Nothing special. But when I search with some words "XXX YYY", the result contains also fields which do not contain any of the words or start with them.
When I get some time, I will check with EXPLANATION.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.