BooleanQueryBuilder is not adding parenthesis around the query

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

BooleanQueryBuilder is not adding parenthesis around the query

gnandre
Hi,

BooleanQueryBuilder is not adding parenthesis around the query. It
only adds + sign at the start of the query but not the parentheses around
the query. Why is that? How should I add it?

booleanQueryBuilder.add(query, BooleanClause.Occur.MUST)
Reply | Threaded
Open this post in threaded view
|

Re: BooleanQueryBuilder is not adding parenthesis around the query

Edward Ribeiro
If you are using Lucene's BooleanQueryBuilder then you need to do nesting
of your queries (roughly, a builder for each query enclosed in
"parenthesis").

A query like (text:child AND text:toys) OR age:12 would be:

Query query1 = new TermQuery(new Term("text", "toys"));
Query query2 = new TermQuery(new Term("text", "children"));
Query query3 = new TermQuery(new Term("age", "12"));

BooleanQuery.Builder andBuilder = new BooleanQuery.Builder();
andBuilder.add(query1, BooleanClause.Occur.MUST);
andBuilder.add(query2, BooleanClause.Occur.MUST);

BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(andBuilder.build(), BooleanClause.Occur.SHOULD);
builder.add(query3, BooleanClause.Occur.SHOULD);

BooleanQuery booleanQuery = builder.build();

This booleanQuery.toString() will be:

(+text:toys +text:children) age:12

That is the parsing of "(text:child AND text:toys) OR age:12"


Edward

On Tue, Jan 21, 2020 at 5:24 PM Arnold Bronley <[hidden email]>
wrote:
>
> Hi,
>
> BooleanQueryBuilder is not adding parenthesis around the query. It
> only adds + sign at the start of the query but not the parentheses around
> the query. Why is that? How should I add it?
>
> booleanQueryBuilder.add(query, BooleanClause.Occur.MUST)
Reply | Threaded
Open this post in threaded view
|

Re: BooleanQueryBuilder is not adding parenthesis around the query

Edward Ribeiro
Oh, you asked about the meaning of the plus sign too.

Well, I recommend reading a book* or any tutorial, but the clauses of
boolean queries there are three occurences, SHOULD, MUST and MUST_NOT, that
roughly translate to OR, AND, and NOT, respectively.

The plus sign means MUST, the minus sign means MUST_NOT and the absence of
both means SHOULD (it may or may not match any indexed term)

For example:

- a query like "text:(toys AND child)" will be translated like "+text:toys
+text:child" (both terms are required to match)

- a query like "text:(toys OR child)" will be translated as "text:toys
text:child" (both terms or only one term can match, that is more or less
equivalent to OR);

- a query like "text:toys NOT text:child" will be translated as "text:toys
-text:child" (try to match text:toys, but also remove the docs that match
text:child from the result set);

* = Lucene book, Solr book and Relevant Search book are excellent resources!

Edward

Em qua, 22 de jan de 2020 15:07, Edward Ribeiro <[hidden email]>
escreveu:

> If you are using Lucene's BooleanQueryBuilder then you need to do nesting
> of your queries (roughly, a builder for each query enclosed in
> "parenthesis").
>
> A query like (text:child AND text:toys) OR age:12 would be:
>
> Query query1 = new TermQuery(new Term("text", "toys"));
> Query query2 = new TermQuery(new Term("text", "children"));
> Query query3 = new TermQuery(new Term("age", "12"));
>
> BooleanQuery.Builder andBuilder = new BooleanQuery.Builder();
> andBuilder.add(query1, BooleanClause.Occur.MUST);
> andBuilder.add(query2, BooleanClause.Occur.MUST);
>
> BooleanQuery.Builder builder = new BooleanQuery.Builder();
> builder.add(andBuilder.build(), BooleanClause.Occur.SHOULD);
> builder.add(query3, BooleanClause.Occur.SHOULD);
>
> BooleanQuery booleanQuery = builder.build();
>
> This booleanQuery.toString() will be:
>
> (+text:toys +text:children) age:12
>
> That is the parsing of "(text:child AND text:toys) OR age:12"
>
>
> Edward
>
> On Tue, Jan 21, 2020 at 5:24 PM Arnold Bronley <[hidden email]>
> wrote:
> >
> > Hi,
> >
> > BooleanQueryBuilder is not adding parenthesis around the query. It
> > only adds + sign at the start of the query but not the parentheses around
> > the query. Why is that? How should I add it?
> >
> > booleanQueryBuilder.add(query, BooleanClause.Occur.MUST)
>
Reply | Threaded
Open this post in threaded view
|

Re: BooleanQueryBuilder is not adding parenthesis around the query

gnandre
In reply to this post by Edward Ribeiro
Thanks, Edaward. This was the exact answer I was looking for :)

On Wed, Jan 22, 2020 at 1:08 PM Edward Ribeiro <[hidden email]>
wrote:

> If you are using Lucene's BooleanQueryBuilder then you need to do nesting
> of your queries (roughly, a builder for each query enclosed in
> "parenthesis").
>
> A query like (text:child AND text:toys) OR age:12 would be:
>
> Query query1 = new TermQuery(new Term("text", "toys"));
> Query query2 = new TermQuery(new Term("text", "children"));
> Query query3 = new TermQuery(new Term("age", "12"));
>
> BooleanQuery.Builder andBuilder = new BooleanQuery.Builder();
> andBuilder.add(query1, BooleanClause.Occur.MUST);
> andBuilder.add(query2, BooleanClause.Occur.MUST);
>
> BooleanQuery.Builder builder = new BooleanQuery.Builder();
> builder.add(andBuilder.build(), BooleanClause.Occur.SHOULD);
> builder.add(query3, BooleanClause.Occur.SHOULD);
>
> BooleanQuery booleanQuery = builder.build();
>
> This booleanQuery.toString() will be:
>
> (+text:toys +text:children) age:12
>
> That is the parsing of "(text:child AND text:toys) OR age:12"
>
>
> Edward
>
> On Tue, Jan 21, 2020 at 5:24 PM Arnold Bronley <[hidden email]>
> wrote:
> >
> > Hi,
> >
> > BooleanQueryBuilder is not adding parenthesis around the query. It
> > only adds + sign at the start of the query but not the parentheses around
> > the query. Why is that? How should I add it?
> >
> > booleanQueryBuilder.add(query, BooleanClause.Occur.MUST)
>
Reply | Threaded
Open this post in threaded view
|

Re: BooleanQueryBuilder is not adding parenthesis around the query

gnandre
I knew about the + and other signs and their connections to MUST and other
operators. What I did not understand was why it was not adding parentheses
around the expression. In your first replay you mentioned that -  'roughly,
a builder for each query enclosed in "parenthesis"' - that was the key
point I was missing.

On Wed, Jan 22, 2020 at 2:40 PM Arnold Bronley <[hidden email]>
wrote:

> Thanks, Edaward. This was the exact answer I was looking for :)
>
> On Wed, Jan 22, 2020 at 1:08 PM Edward Ribeiro <[hidden email]>
> wrote:
>
>> If you are using Lucene's BooleanQueryBuilder then you need to do nesting
>> of your queries (roughly, a builder for each query enclosed in
>> "parenthesis").
>>
>> A query like (text:child AND text:toys) OR age:12 would be:
>>
>> Query query1 = new TermQuery(new Term("text", "toys"));
>> Query query2 = new TermQuery(new Term("text", "children"));
>> Query query3 = new TermQuery(new Term("age", "12"));
>>
>> BooleanQuery.Builder andBuilder = new BooleanQuery.Builder();
>> andBuilder.add(query1, BooleanClause.Occur.MUST);
>> andBuilder.add(query2, BooleanClause.Occur.MUST);
>>
>> BooleanQuery.Builder builder = new BooleanQuery.Builder();
>> builder.add(andBuilder.build(), BooleanClause.Occur.SHOULD);
>> builder.add(query3, BooleanClause.Occur.SHOULD);
>>
>> BooleanQuery booleanQuery = builder.build();
>>
>> This booleanQuery.toString() will be:
>>
>> (+text:toys +text:children) age:12
>>
>> That is the parsing of "(text:child AND text:toys) OR age:12"
>>
>>
>> Edward
>>
>> On Tue, Jan 21, 2020 at 5:24 PM Arnold Bronley <[hidden email]>
>> wrote:
>> >
>> > Hi,
>> >
>> > BooleanQueryBuilder is not adding parenthesis around the query. It
>> > only adds + sign at the start of the query but not the parentheses
>> around
>> > the query. Why is that? How should I add it?
>> >
>> > booleanQueryBuilder.add(query, BooleanClause.Occur.MUST)
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: BooleanQueryBuilder is not adding parenthesis around the query

Edward Ribeiro
Cool. Glad to help. :)

Cheers,
Edward

Em qua, 22 de jan de 2020 16:44, Arnold Bronley <[hidden email]>
escreveu:

> I knew about the + and other signs and their connections to MUST and other
> operators. What I did not understand was why it was not adding parentheses
> around the expression. In your first replay you mentioned that -  'roughly,
> a builder for each query enclosed in "parenthesis"' - that was the key
> point I was missing.
>
> On Wed, Jan 22, 2020 at 2:40 PM Arnold Bronley <[hidden email]>
> wrote:
>
> > Thanks, Edaward. This was the exact answer I was looking for :)
> >
> > On Wed, Jan 22, 2020 at 1:08 PM Edward Ribeiro <[hidden email]
> >
> > wrote:
> >
> >> If you are using Lucene's BooleanQueryBuilder then you need to do
> nesting
> >> of your queries (roughly, a builder for each query enclosed in
> >> "parenthesis").
> >>
> >> A query like (text:child AND text:toys) OR age:12 would be:
> >>
> >> Query query1 = new TermQuery(new Term("text", "toys"));
> >> Query query2 = new TermQuery(new Term("text", "children"));
> >> Query query3 = new TermQuery(new Term("age", "12"));
> >>
> >> BooleanQuery.Builder andBuilder = new BooleanQuery.Builder();
> >> andBuilder.add(query1, BooleanClause.Occur.MUST);
> >> andBuilder.add(query2, BooleanClause.Occur.MUST);
> >>
> >> BooleanQuery.Builder builder = new BooleanQuery.Builder();
> >> builder.add(andBuilder.build(), BooleanClause.Occur.SHOULD);
> >> builder.add(query3, BooleanClause.Occur.SHOULD);
> >>
> >> BooleanQuery booleanQuery = builder.build();
> >>
> >> This booleanQuery.toString() will be:
> >>
> >> (+text:toys +text:children) age:12
> >>
> >> That is the parsing of "(text:child AND text:toys) OR age:12"
> >>
> >>
> >> Edward
> >>
> >> On Tue, Jan 21, 2020 at 5:24 PM Arnold Bronley <[hidden email]
> >
> >> wrote:
> >> >
> >> > Hi,
> >> >
> >> > BooleanQueryBuilder is not adding parenthesis around the query. It
> >> > only adds + sign at the start of the query but not the parentheses
> >> around
> >> > the query. Why is that? How should I add it?
> >> >
> >> > booleanQueryBuilder.add(query, BooleanClause.Occur.MUST)
> >>
> >
>