3

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?

1 Answer 1

4

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.