lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ian Lea <ian....@gmail.com>
Subject Re: BooleanQuery and NOT-Operator
Date Thu, 03 Nov 2011 14:58:34 GMT
I guess you're confusing it with MUST (MUST_NOT) logic.  Try creating
the query you want to exclude without MUST_NOT and add it to the
BooleanQuery with MUST_NOT.  That sounds very confusing.

For example

BooleanQuery bq = new BooleanQuery();
TermQuery tqwanted = new TermQuery(new Term("field1: lovely"));
TermQuery tqexclude = new TermQuery(new Term("field2: horrible"));
bq.add(tqwanted, Occur.MUST);
bq.add(tqexclude, Occur.MUST_NOT);

Should also work with more complex queries.


--
Ian.


On Thu, Nov 3, 2011 at 2:31 PM, Kolhoff, Jacqueline - ENCOWAY
<Kolhoff@encoway.de> wrote:
> Hi,
>
> I have a problem when using BooleanQuery with NOT-Operators.
>
> When I want to search my documents for elements where a special field is NOT a special
value AND another field is a special value, I do the following in my code:
>
> Query query1 = ...; // -field1:value1
> Query query2=...;  // field2:*value2*
>
> I combine them via a BooleanQuery like this:
>
> BooleanQuery booleanQuery = new BooleanQuery();
> booleanQuery.add(query1, Occur.MUST);
> booleanQuery.add(query2, Occur.MUST);
>
> This generates a query like this: +(- field1:value1) +field2:*value2*
>
> which does not work because of the brackets around the NOT part. What can I do to prevent
this?
>
> It could also be possible that query1 is more complex like this:
> Query query1 = ...; // -field1:value1 AND field3:value3
>
> which finally leads to:  +(+(- field1:value1) +(field3:value3)) +field2:*value2*
>
> How can I solve this? Maybe there are some special queries or strategies when using NOT
operators?
>
> Thanks and regards,
> Jacqueline.
>
>
>

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


Mime
View raw message