Lucene - Query Programming-Boolean Query - Lucene

What is Lucene Boolean Query?

BooleanQuery is used to search documents which are a result of multiple queries using AND, OR or NOT operators.

Class Declaration

Following is the declaration for the class −


Following is the field for the BooleanQuery −

  • protected int minNrShouldMatch

Class Constructors

The following table shows the different class constructors −

S.No. Constructor & Description
Constructs an empty Boolean query.
BooleanQuery(boolean disableCoord)
Constructs an empty Boolean query.

Class Methods

The following table shows the different class methods −

S.No. Method & Description
void add(BooleanClause clause)
Adds a clause to a Boolean query.
void add(Query query, BooleanClause.Occur occur)
Adds a clause to a boolean query.
List<BooleanClause> clauses()
Returns the list of clauses in this query.
Object clone()
Returns a clone of this query.
Weight createWeight(Searcher searcher)
Expert: Constructs an appropriate Weight implementation for this query.
boolean equals(Object o)
Returns true if object o is equal to this.
void extractTerms(Setterms)
Expert: Adds all terms occurring in this query to the terms set.
BooleanClause[] getClauses()
Returns the set of clauses in this query.
static int getMaxClauseCount()
Returns the maximum number of clauses permitted, 1024 by default.
int getMinimumNumberShouldMatch()
Gets the minimum number of the optional BooleanClauses which must be satisfied.
int hashCode()
Returns a hash code value for this object.
boolean isCoordDisabled()
Returns true ifSimilarity.coord(int,int)is disabled in scoring for this query instance.
Iterator<BooleanClause> iterator()
Returns an iterator on the clauses in this query.
Query rewrite(IndexReader reader)
Expert: Called to re-write queries into primitive queries.
static void setMaxClauseCount(int maxClauseCount)
Sets the maximum number of clauses permitted per BooleanQuery.
void setMinimumNumberShouldMatch(int min)
Specifies a minimum number of the optional BooleanClauses which must be satisfied.
String toString(String field)
Prints a user-readable version of this query.

Methods Inherited

This class inherits methods from the following classes −

  • java.lang.Object


Example Application

Let us create a test Lucene application to test search using BooleanQuery.

Step Description
Create a project with a nameLuceneFirstApplicationunder a packagecom.wisdomjobs.luceneas explained in theLucene - First Applicationchapter. You can also use the project created inLucene - First Applicationchapter as such for this chapter to understand the searching process.
CreateLuceneConstants.javaandSearcher.javaas explained in theLucene - First Applicationchapter. Keep the rest of the files unchanged.
CreateLuceneTester.javaas mentioned below.
Clean and Build the application to make sure the business logic is working as per the requirements.

This class is used to provide various constants to be used across the sample application.

This class is used to read the indexes made on raw data and searches data using the Lucene library.

This class is used to test the searching capability of the Lucene library.

Data & Index Directory Creation

We have used 10 text files from record1.txt to record10.txt containing names and other details of the students and put them in the directory E:\Lucene\Data. Test Data. An index directory path should be created as E:\Lucene\Index. After running the indexing program during chapter Lucene - Indexing Process, you can see the list of index files created in that folder.

Running the Program

Once you are done with the creation of the source, the raw data, the data directory, the index directory and the indexes, you can proceed by compiling and running your program. To do this, keep the LuceneTester.Java file tab active and use either the Run option available in the Eclipse IDE or use Ctrl + F11 to compile and run your LuceneTester application. If everything is fine with your application, this will print the following message in Eclipse IDE's console −

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Lucene Topics