Lucene 6.0! How to instantiate a BooleanQuery and add other search queries in it?
Asked Answered
C

1

15

How to instantiate a BooleanQuery in Lucene 6.x ? How to use Boolean Query to add other queries in it ?

In Lucene 4.x we use BooleanQuery as follow:

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

How this can be achieved in Lucene 6.

Croteau answered 19/6, 2016 at 7:35 Comment(0)
O
28

BooleanQuery is now immutable (you can read about the change in the Migration guide and the linked JIRA issues).

Instead, you would now use BooleanQuery.Builder:

BooleanQuery booleanQuery = new BooleanQuery.Builder()
    .add(query1, BooleanClause.Occur.MUST)
    .add(query2, BooleanClause.Occur.MUST)
    .build();
Obmutescence answered 19/6, 2016 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.