Lucene BooleanQuery is depreacted as of 5.3.0

Lucene API changes pretty fast, it's a good news or bad news? For beginners, it can be very confusing when running example code, because if you run the code with wrong version, there are many little changes you need to do in the code.

Part of the reasons maybe the Lucene codebase is not well designed in it's early stages. Because many of the changes are superficial, name of API, calling conventions, and parameters adjustment. It looks like the team are paying technical debt for Lucene.

Good news is the codebase is getting better and better, getting more mature.

In Lucene 4.0.0, create BooleanQuery this way

 
BooleanQuery booleanQuery = new BooleanQuery();
            booleanQuery.add(query, Occur.MUST);
            booleanQuery.add(query2, Occur.SHOULD);
            booleanQuery.add(query3, Occur.SHOULD);
 
 

In 5.3.0, this is deprecated. Instead it use the builder design pattern to create BooleanQuery instances.

Because BooleanQuery now immutable. The equivalent version in Lucene 5 will be

 
BooleanQuery booleanQuery = new BooleanQuery.Builder()
            .add(query, Occur.MUST)
            .add(query2, Occur.SHOULD)
            .add(query, Occur.SHOULD).build();
 
 

The old version will be removed from Lucene 6.

Here is an Gradle example project using Lucene 5.0.0

Gradle

 
 
ext.luceneVersion= "5.3.0"
 
 
dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
 
    compile "org.apache.lucene:lucene-core:${luceneVersion}"
    compile "org.apache.lucene:lucene-analyzers-common:${luceneVersion}"
    compile "org.apache.lucene:lucene-queryparser:${luceneVersion}"
 
    testCompile group: 'junit', name: 'junit', version: '4.+'
}
 

The code example:

 
package com.makble.luceneexample;
 
import java.io.IOException;
 
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanQuery.Builder;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.RAMDirectory;
 
public class Lucene5Test {
 
    public static Analyzer analyzer = new StandardAnalyzer();
    public static IndexWriterConfig config = new IndexWriterConfig(analyzer);
    public static RAMDirectory ramDirectory = new RAMDirectory();
    public static IndexWriter indexWriter;
 
 
    public static void main (String args[]) {
 
        ramDirectory = new RAMDirectory();
        createIndexWithoutBoost();
        searchIndexWithQueryBoost();
    }
        public static void searchIndexWithQueryBoost() {
        try {
            IndexReader idxReader = DirectoryReader.open(ramDirectory);
            IndexSearcher idxSearcher = new IndexSearcher(idxReader);
 
            TermQuery query = new TermQuery(new Term("title", "fox"));
            TermQuery query2 = new TermQuery(new Term("author", "smith"));
            TermQuery query3 = new TermQuery(new Term("author", "sam"));
            query2.setBoost((float) 2.0);
 
            BooleanQuery booleanQuery = new BooleanQuery.Builder()
            .add(query, Occur.MUST)
            .add(query2, Occur.SHOULD)
            .add(query, Occur.SHOULD).build();//new BooleanQuery();
 
            TopDocs docs = idxSearcher.search(booleanQuery, 10);
            System.out.println ("length of top docs: " + docs.scoreDocs.length);
            for( ScoreDoc doc : docs.scoreDocs) {
                Document thisDoc = idxSearcher.doc(doc.doc);
                System.out.println(doc.doc + "\t" + thisDoc.get("author") + "\t" + thisDoc.get("title"));
                Explanation explanation = idxSearcher.explain(booleanQuery, doc.doc);
                System.out.println("----------");
                System.out.println(explanation.toString());
                System.out.println("----------");
                System.out.println("");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            ramDirectory.close();
        }
    }
    public static void createDoc(String author, String title) throws IOException {
        Document doc = new Document();
        doc.add(new TextField("author", author, Field.Store.YES));
        doc.add(new TextField("title", title, Field.Store.YES));
 
        indexWriter.addDocument(doc);
    }
 
    public static void createIndexWithoutBoost() {
        try {
                indexWriter = new IndexWriter(ramDirectory, config);    
                createDoc("Sam", "brown fox jump");    
                createDoc("Sam", "fox jumps over lazy dog");    
                createDoc("Jack", "brown fox jump");
                createDoc("Smith", "brown fox jump");
                createDoc("Smith", "brown fox jumps over lazy dog");
 
                indexWriter.close();
        } catch (IOException | NullPointerException ex) {
            System.out.println("Exception : " + ex.getLocalizedMessage());
        } 
    }
 
}