Pokies Pokies - 6 months ago 60
Java Question

How to use a BooleanQuery builder in Lucene 5.3.x?

I'm almost new to lucene and trying to AND some queries and display them.
I've googled for entire web, But can't find the correct solution to this problem. The solutions for boolean query search include

BooleanQuery
lucene class. But this class is deprecated in lucene 5.3.1 (which i'm using) Here

This is a part of my code:

public static void searchBooleanQuery(String indexDir, Query query1,
Query query2, Query query3, Query query4) throws IOException {
IndexReader rdr = DirectoryReader.open(FSDirectory.open(Paths.get(indexDir)));
IndexSearcher is = new IndexSearcher(rdr);
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
booleanQuery.add(query1, BooleanClause.Occur.MUST);
booleanQuery.add(query2, BooleanClause.Occur.MUST);
booleanQuery.add(query3, BooleanClause.Occur.MUST);
booleanQuery.add(query4, BooleanClause.Occur.MUST);
}


Update

The problem :
I can not display this
Boolean Query
by
IndexSearcher
Object, Because search method of this class (IndexSearcher) should be passed by a Query! So it gives me an error when i'm trying to run this code:

TopDocs hits = is.search(booleanQuery,10);
...

Answer

Your booleanQuery object is actually an instance of BooleanQuery.Builder, not BooleanQuery.

After you're done adding all your queries to the builder, you need to call the build method.

Ex.

TopDocs hits = is.search(booleanQuery.build(),10);