You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by heikki <tr...@gmail.com> on 2012/08/23 14:09:22 UTC

Question about BooleanQuery

hello,

I'm trying to construct a boolean query, but can't get it to return the
results that I intend. Does anyone see what I'm doing wrong ?

The query is like



The idea is that it should return documents where

someField_1 = 0 OR someField_2 = 0 AND booleanField_1 = false BUT NOT (
someField_1 = 0 OR someField_2 = 0) AND booleanField_2 = true )

The problem seems to be with the BUT NOT part of the query. I get no
results, even though I would have expected so (and I've checked the index
contents with Luke).

Any ideas most welcome ..
Kind regards
Heikki Doeleman



--
View this message in context: http://lucene.472066.n3.nabble.com/Question-about-BooleanQuery-tp4002822.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question about BooleanQuery

Posted by Jack Krupansky <ja...@basetechnology.com>.
And "(NOT booleanField_2 = true )" is really just "booleanField_2 = false", 
right? Unless you are looking for fields that are not populated with any 
value in addition to an explicit false value.

-- Jack Krupansky

-----Original Message----- 
From: heikki
Sent: Thursday, August 23, 2012 9:13 AM
To: java-user@lucene.apache.org
Subject: Re: Question about BooleanQuery

OK, it's not the idea that the nested NOT query has got anything to do with
booleanField_1, so I'll try to phrase very clearly what I want :

the query should return docs where

( someField_1 = 0 OR someField_2 = 0) AND
( booleanField_1 = false ) AND
( NOT ( ( someField_1 = 0 OR someField_2 = 0 ) AND booleanField_2 = true ) )

So, all docs that have value 0 for someField_1 or someField_2, and that have
value false for booleanField_1, but excluded from this, those docs that have
value 0 for someField_1 or someField_2 and value true for booleanField_2.

Come to think of it, it's probably useless to repeat the someFields clause
in the NOT clause, so I'll try simplifying this to

(someField_1 = 0 OR someField_2 = 0) AND
(booleanField_1 = false) AND
(NOT booleanField_2 = true )


Even so, I don't see why my original query doesn't return results.



--
View this message in context: 
http://lucene.472066.n3.nabble.com/Question-about-BooleanQuery-tp4002822p4002854.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question about BooleanQuery

Posted by heikki <tr...@gmail.com>.
OK, it's not the idea that the nested NOT query has got anything to do with
booleanField_1, so I'll try to phrase very clearly what I want :

the query should return docs where

( someField_1 = 0 OR someField_2 = 0) AND
( booleanField_1 = false ) AND 
( NOT ( ( someField_1 = 0 OR someField_2 = 0 ) AND booleanField_2 = true ) )

So, all docs that have value 0 for someField_1 or someField_2, and that have
value false for booleanField_1, but excluded from this, those docs that have
value 0 for someField_1 or someField_2 and value true for booleanField_2.

Come to think of it, it's probably useless to repeat the someFields clause
in the NOT clause, so I'll try simplifying this to 

(someField_1 = 0 OR someField_2 = 0) AND
(booleanField_1 = false) AND 
(NOT booleanField_2 = true )


Even so, I don't see why my original query doesn't return results.



--
View this message in context: http://lucene.472066.n3.nabble.com/Question-about-BooleanQuery-tp4002822p4002854.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question about BooleanQuery

Posted by Jack Krupansky <ja...@basetechnology.com>.
Ah... the way you've phrased it, the nested NOT query will exclude docs even 
if "booleanField_1:false", so that will guarantee zero results.

It seems like what you want is:

+someField_1:0
+someField_2:0
+booleanField_1:false
-booleanField_2:true

In your original case, the parentheses were causing the problem since your 
nested query expression was not fully specified - it needed 
booleanField_1:true.

But, of course, I am guessing what you really want.

-- Jack Krupansky

-----Original Message----- 
From: heikki
Sent: Thursday, August 23, 2012 8:38 AM
To: java-user@lucene.apache.org
Subject: Re: Question about BooleanQuery

thanks Jack for your answer, however I'm not quite sure what to do with it:

the query is like


  +( someField_1:0 someField_2:0 )
  +booleanField_1:false
  -(
      +( someField_1:0 someField_2:0 )
      +booleanField_2:true
    )

(I put this in 'raw' before, think it might not have shown up in all ways of
viewing this list).

I don't think there is a mis-matched bracket at the end (there was in my
textual description of this though), and I also don't see how this can be
more fully bracketed.

Kind regards
Heikki Doeleman



--
View this message in context: 
http://lucene.472066.n3.nabble.com/Question-about-BooleanQuery-tp4002822p4002836.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question about BooleanQuery

Posted by heikki <tr...@gmail.com>.
thanks Jack for your answer, however I'm not quite sure what to do with it:

the query is like


  +( someField_1:0 someField_2:0 )
  +booleanField_1:false
  -( 
      +( someField_1:0 someField_2:0 )
      +booleanField_2:true
    )

(I put this in 'raw' before, think it might not have shown up in all ways of
viewing this list).

I don't think there is a mis-matched bracket at the end (there was in my
textual description of this though), and I also don't see how this can be
more fully bracketed.

Kind regards
Heikki Doeleman



--
View this message in context: http://lucene.472066.n3.nabble.com/Question-about-BooleanQuery-tp4002822p4002836.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question about BooleanQuery

Posted by Jack Krupansky <ja...@basetechnology.com>.
Step 1, fully parenthesize your boolean to show your desired order of 
execution. The Lucene BooleanQuery does not do a pure Boolean evaluation. 
You have the same sub-expression in your NOT clause - that's probably what 
guarantees zero results. And you have an unmatched right parenthesis at the 
end.

Lucene BooleanQuery uses "SHOULD" (for "OR"), "MUST" (for "AND"), and 
"MUST_NOT" ("NOT"). Very similar to a true Boolean expression except that 
the clauses at any level of expression are not evaluated in order or AND/OR 
precedence. If you need strict evaluation order, use parentheses (or 
multi-level BooleanQuery at the Lucene Query level.)

Read more detail at:
http://searchhub.org/dev/2011/12/28/why-not-and-or-and-not/

-- Jack Krupansky

-----Original Message----- 
From: heikki
Sent: Thursday, August 23, 2012 8:09 AM
To: java-user@lucene.apache.org
Subject: Question about BooleanQuery

hello,

I'm trying to construct a boolean query, but can't get it to return the
results that I intend. Does anyone see what I'm doing wrong ?

The query is like



The idea is that it should return documents where

someField_1 = 0 OR someField_2 = 0 AND booleanField_1 = false BUT NOT (
someField_1 = 0 OR someField_2 = 0) AND booleanField_2 = true )

The problem seems to be with the BUT NOT part of the query. I get no
results, even though I would have expected so (and I've checked the index
contents with Luke).

Any ideas most welcome ..
Kind regards
Heikki Doeleman



--
View this message in context: 
http://lucene.472066.n3.nabble.com/Question-about-BooleanQuery-tp4002822.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org