lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jack Krupansky <jack.krupan...@gmail.com>
Subject Re: Creating composite query in lucene
Date Tue, 08 Mar 2016 15:02:54 GMT
BooleanQuery can be nested, so you do a top-level BQ that has two clauses,
the first a TQ for a:x and the second another BQ that itself has two
clauses, both SHOULD.

-- Jack Krupansky

On Tue, Mar 8, 2016 at 4:38 AM, sandeep das <yarnhadoop@gmail.com> wrote:

> Hi,
>
> I'm using lucene-5.2.0 and in query interface I wish to compose a query
> like
> "a=x and (b=y or d=z)"
>
>
> Which can be described as If any document has value "x" for field "a" and
> field "b" has value "y" or field "d" has value "z" then that document
> should be chosen. There are three fields in my document i.e. a, b and c.
>
> I was thinking of using BooleanQuery object to implement such query but it
> seems difficult to implement the same.
>
> If I write the above clause in terms of boolean query then this is the best
> I can think of:
>
> ((BooleanQuery)query).add(new RegexpQuery(new Term("a", "x")),
> BooleanClause.Occur.MUST);
>
> ((BooleanQuery)query).add(new RegexpQuery(new Term("b", "y")),
> BooleanClause.Occur.SHOULD);
>
> ((BooleanQuery)query).add(new RegexpQuery(new Term("c", "z")),
> BooleanClause.Occur.SHOULD);
>
>
> But in the above case a document will be selected even if it does not have
> value of field "b" as "y" or value of field "c" as "z" but has value of
> field "a" as "x". The OR condition might be ignored.
>
> Correct me If my understanding is wrong here.
>
> Can someone please suggest some better solution to compose such query?
>
> Regards,
> Sandeep
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message