lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ian Lea <>
Subject Re: Grouping Clauses to Preserve Order of Boolean Precedence
Date Wed, 03 Aug 2011 13:54:58 GMT
I don't think there is an easy way.  Brackets are the official way to
do it with the query parser:

For anything non-trivial I prefer to build up queries in code using
BooleanQuery.  That way it is comparatively easy to build in whatever
logic you need with BooleanClause.Occur.

There are alternative parsers in contrib.  They might have more
support for grouping clauses.


On Wed, Aug 3, 2011 at 2:26 PM, Jim Swainston
<> wrote:
> Hi,
> I'm having trouble thinking of a way to effectively group clauses to form
> sub queries. For example, I need to handle the following query:
> Marketing AND Smith OR Davies.
> Lucene is currently parsing this as  +Marketing +Smith Davies meaning that
> results where only the term Davies is found are not returned. I want to be
> able to apply the order of Boolean precedence so that this query is treated
> as:
> (Marketing AND Smith) OR Davies.
> The QueryParser will correctly parse the above query as (+Marketing +Smith)
> Davies meaning that results where only Davies is found will be returned.
> Is there a way within Lucene to apply this order of precedence? Currently
> the only way I can think of is to manually add the brackets in the correct
> place. However, this would become more difficult as the number of clauses
> increases so I'm not sure how scalable this method would be.
> How would I work out where to place the brackets if the query was something
> like:
> Marketing AND Smith OR Davies OR Management OR Business AND
> Science.........?
> Can any suggest an effective way to group clauses so that the order of
> Boolean precedence is preserved?
> Thanks very much.
> Jim

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message