Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, though could'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 (the one that I'm using)

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 cannot display Boolean Query by IndexSearcher Object as search method of this class (IndexSearcher) should be passed to by a Query! So, it gives me an error when I'm trying to run the following:

TopDocs hits = is.search(booleanQuery,10);
...
like image 901
inverted_index Avatar asked Oct 30 '15 15:10

inverted_index


1 Answers

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);
like image 55
user1071777 Avatar answered Oct 20 '22 07:10

user1071777