Re: Creating composite query in lucene
Hi Jack, Thanks a lot for your suggestion. Regards, Sandeep On Tue, Mar 8, 2016 at 8:32 PM, Jack Krupanskywrote: > 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 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 > > >
Re: Creating composite query in lucene
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 daswrote: > 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 >
Creating composite query in lucene
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