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 Ian Koelliker <IK...@axsone.com> on 2015/02/06 22:46:43 UTC

Lucene query behavior using NOT

Hello,
I am trying to understand whether I am using the NOT operator correctly. I have the following scenario:

Query 1 = body:(a OR NOT b)

This is parsed as:  (body:a) -(body:b) and finds 96,620 hits

Query 2 = body:(a OR (*:* AND NOT b))

This is parsed as: (body:a) (+*:* -(body:b)) and finds 149,438 hits

Query 3 = body:(a AND NOT b)

This is parsed as:  +(body:a) -(body:b) and finds 96,620 hits

Query 4 = body:(a AND (*:* AND NOT b))

This is parsed as:  +(body:a) +(+*:* -(body:b)) and finds the same 96,620 hits

Does anyone know the nuances when working with the NOT operator in terms of why query 1 and query 2 return different hits whereas query 3 and query 4 return the same? Is there a correct usage when you want to use NOT with AND/OR?

Thanks,
Ian

Re: Lucene query behavior using NOT

Posted by Trejkaz <tr...@trypticon.org>.
On Sun, Feb 8, 2015 at 9:04 PM, Uwe Schindler <uw...@thetaphi.de> wrote:
> Hi,
>
> Lucene does not use algebraic / boolean logic! Maybe review this blog
> post: https://lucidworks.com/blog/why-not-and-or-and-not/

This article is an old classic.

The "plus, minus, nothing" operators aren't without their issues
either, though. For example, if you are stuck in an environment where
someone else thought it was a great idea to set the "default operator"
in the query parser to AND, you might wonder what the syntax is for
this third example:

    fish chips
        -> MUST match fish, MUST match chips

    +fish +chips
        -> MUST match fish, MUST match chips

    ????
        -> SHOULD match fish, SHOULD match chips

Yeah, I don't know either.

In any case, the issues in the boolean syntax can be avoided:
  * Always use parentheses when mixing different operators (or
possibly use PrecedenceQueryParser. I haven't tried it.)
  * To any boolean query with only NOT clause, transform it to add a
SHOULD clause for MatchAllDocumentsQuery.


TX

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


RE: Lucene query behavior using NOT

Posted by Uwe Schindler <uw...@thetaphi.de>.
Hi,

Lucene does not use algebraic / boolean logic! Maybe review this blog post: https://lucidworks.com/blog/why-not-and-or-and-not/

As you see in your examples and how the wqueries are parsed, AND, OR, and NOT are mapped to something else: Lucene only knows if a term MUST be in results, or SHOULD be in results (at least one occurrence in total), or MUST_NOT. The Operators are applied to the clauses (terms) in Lucene.

Uwe

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Ian Koelliker [mailto:IKoelliker@axsone.com]
> Sent: Friday, February 06, 2015 10:47 PM
> To: java-user@lucene.apache.org
> Subject: Lucene query behavior using NOT
> 
> Hello,
> I am trying to understand whether I am using the NOT operator correctly. I
> have the following scenario:
> 
> Query 1 = body:(a OR NOT b)
> 
> This is parsed as:  (body:a) -(body:b) and finds 96,620 hits
> 
> Query 2 = body:(a OR (*:* AND NOT b))
> 
> This is parsed as: (body:a) (+*:* -(body:b)) and finds 149,438 hits
> 
> Query 3 = body:(a AND NOT b)
> 
> This is parsed as:  +(body:a) -(body:b) and finds 96,620 hits
> 
> Query 4 = body:(a AND (*:* AND NOT b))
> 
> This is parsed as:  +(body:a) +(+*:* -(body:b)) and finds the same 96,620 hits
> 
> Does anyone know the nuances when working with the NOT operator in
> terms of why query 1 and query 2 return different hits whereas query 3 and
> query 4 return the same? Is there a correct usage when you want to use NOT
> with AND/OR?
> 
> Thanks,
> Ian


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