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 "Kolhoff, Jacqueline - ENCOWAY" <Ko...@encoway.de> on 2011/11/03 15:31:26 UTC
BooleanQuery and NOT-Operator
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.
Re: BooleanQuery and NOT-Operator
Posted by Ian Lea <ia...@gmail.com>.
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
<Ko...@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