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 Nabib El-Rahman <na...@gmail.com> on 2010/11/24 02:04:44 UTC

Not query help.

Hi,

What I need is a Not TermQuery. I did not see one in the API, so I did the
following:

Query query = new BooleanQuery(new BooleanClause(new
TermQuery(..), BooleanClause.Occur.MUST_NOT)));

This did not produce the correct search result. Does anyone have any idea on
how to accomplish this?

And is here such a thing as NotTermQuery(..) ?

Thanks in advance!

-Nabib

Re: Not query help.

Posted by Mark Kristensson <ma...@smartsheet.com>.
I've had to deal with exactly this same scenario and, from what I know,
there is no equivalent in Lucene to a SQL != kind of query. So, you have to
have some way to select "everything" (whatever that means in your scenario)
and then remove the stuff that you don't want (the NOT portion). For my
application, I have a user permission field that I can use to select
everything that a user has access to and then I can "NOT out" the stuff
specified by the != portion of the query. These, of course, are two queries
that I AND together with a BooleanQuery.

-Mark

On Tue, Nov 23, 2010 at 5:04 PM, Nabib El-Rahman <na...@gmail.com> wrote:

> Hi,
>
> What I need is a Not TermQuery. I did not see one in the API, so I did the
> following:
>
> Query query = new BooleanQuery(new BooleanClause(new
> TermQuery(..), BooleanClause.Occur.MUST_NOT)));
>
> This did not produce the correct search result. Does anyone have any idea
> on
> how to accomplish this?
>
> And is here such a thing as NotTermQuery(..) ?
>
> Thanks in advance!
>
> -Nabib
>

RE: Not query help.

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

Do it like that:

BooleanQuery bq = new BooleanQuery();
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
bq.add(new TermQuery(..), BooleanClause.Occur.MUST_NOT);

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


> -----Original Message-----
> From: Nabib El-Rahman [mailto:nabiber@gmail.com]
> Sent: Wednesday, November 24, 2010 2:05 AM
> To: java-user@lucene.apache.org
> Subject: Not query help.
> 
> Hi,
> 
> What I need is a Not TermQuery. I did not see one in the API, so I did the
> following:
> 
> Query query = new BooleanQuery(new BooleanClause(new TermQuery(..),
> BooleanClause.Occur.MUST_NOT)));
> 
> This did not produce the correct search result. Does anyone have any idea
on
> how to accomplish this?
> 
> And is here such a thing as NotTermQuery(..) ?
> 
> Thanks in advance!
> 
> -Nabib


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