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 <arnoldbron...@gmail.com>
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 via email to