Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BooleanQuery.combine using the BooleanQuery.Builder in Lucene

Tags:

java

lucene

Let's assume that we have the following Lucene query:

+(f1:x f2:x) +f3:y

This means that the result must contain the value "x" in the field "f1" or the field "f2" and the field "f3" must contain the value "y";

In old versions of Lucene, BooleanQuery offers a combine method that allows you to put parenthesis where you want.

Now in the newest version of Lucene, the combine method was removed and you have to go through the new BooleanQuery.builder.

The problem is that the only way you can add clauses to a builder, is to "add" them with the matching Occur needed. There is no way to assemble clauses between parenthesis.

Does anybody know how to achieve this simple query using this new builder?

like image 903
SBA Avatar asked Mar 04 '26 08:03

SBA


1 Answers

You can use BooleanQuery objects in BooleanQuery.Builder#add

For your query +(f1:x f2:x) +f3:y:

BooleanQuery.Builder finalQuery = new BooleanQuery.Builder();
BooleanQuery.Builder q1 = new BooleanQuery.Builder();
q1.add(new TermQuery(new Term("f1", "x")), Occur.SHOULD);
q1.add(new TermQuery(new Term("f2", "x")), Occur.SHOULD);
finalQuery.add(q1.build(), Occur.MUST);
finalQuery.add(new TermQuery(new Term("f3", "y")), Occur.MUST);
Query queryForSearching = finalQuery.build();
like image 136
user1071777 Avatar answered Mar 06 '26 20:03

user1071777



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!