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 Michael Pickard <mp...@visitlondon.com> on 2006/01/25 13:32:10 UTC
Help needed with BooleanQuery formation
Can anyone help me with the formation of a BooleanQuery ?
I want a query of the form:
x AND ( a OR b OR c OR d)
The nearest I've managed to get is
query.add(new TermQuery(new Term(2, "x")),true,false);
Term term = null;
for (int i=1; i<numberOfNodes; i++){
term = new Term(IndexLn2.CATEGORY_NODE, (String) nodes.get(i));
query.add(new TermQuery(term),false,false);
}
But this results in a query something similar to:
x AND a OR b OR c OR d
Close, but no cigar.
Thanks in advance,
Mike
Michael Pickard
Software Engineer
Visit London
6th Floor,
2 More London Riverside, London, SE1 2RR
Tel: +44 (0)20 7234 5857
Fax: +44 (0)20 7234 5751
www.visitlondon.com
Visit London is the official visitor organisation for London. Visit London is partly funded by Partnership, the Mayor's London Development Agency and the Association of London Government.
The information contained in this e-mail is confidential and intended for the named recipient(s) only. If you have received it in error, please notify the sender immediately and then delete the message. If you are not the intended recipient, you must not use, disclose, copy or distribute this email. The views expressed in this e-mail are those of the individual and not of Visit London. We reserve the right to read and monitor any email or attachment entering or leaving our systems without prior notice.
Re: Help needed with BooleanQuery formation
Posted by Chris Hostetter <ho...@fucit.org>.
: I want a query of the form:
:
: x AND ( a OR b OR c OR d)
what your code is currenlty doing is adding 5 term queries to a single
boolean query.
The structure you want is not a single boolean query, it's a boolean query
containing two mandatory clauses: the first being a term query, and the
second being a boolean query containing 4 optional clauses.
The fact that you needed parens to clearly express what you wanted is the
first tip off.
Another good way to udnerstand how to build a query progromatically like
this, is to try feeding your boolean expression to the query parser, and
then looking at the toString of the query it produces.
: The nearest I've managed to get is
...
: But this results in a query something similar to:
:
: x AND a OR b OR c OR d
Technically, i don't think the query you've created in your java code can
be represented using simple AND OR expressions ... that's why i hate
writing queries out that way, because lucene queries aren't simple boolean
logic constructs, they have scores and relevancy, the best way to describe
what your java code does is...
+x a b c d
...x is mandatory, all other terms are optional and increase the score.
what you want is...
+x +(a b c d)
...x is mandatory, at least one from the list of a, b, c, or d are
mandatory as well, and if more then one match the score is increased
accordingly.
-Hoss
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org
Re: Help needed with BooleanQuery formation
Posted by "Michael D. Curtin" <mi...@curtin.com>.
Michael Pickard wrote:
>
> Can anyone help me with the formation of a BooleanQuery ?
>
> I want a query of the form:
>
> x AND ( a OR b OR c OR d)
You're going to need 2 BooleanQuery objects, one for the OR'd expression
in parentheses, and another for the AND and expression. Something like
this:
BooleanQuery bq_or = new BooleanQuery();
bq_or.add(new TermQuery(new Term("f", "a")), false, false);
bq_or.add(new TermQuery(new Term("f", "b")), false, false);
bq_or.add(new TermQuery(new Term("f", "c")), false, false);
bq_or.add(new TermQuery(new Term("f", "d")), false, false);
BooleanQuery bq_and = new BooleanQuery();
bq_and.add(bq_or, true, false);
bq_and.add(new TermQuery(new Term("f", "x")), true, false);
...
search(bq_and, ...);
Good luck!
--MDC
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org