
- Lucene - Home
- Lucene - Overview
- Lucene - Environment Setup
- Lucene - First Application
- Lucene - Indexing Classes
- Lucene - Searching Classes
- Lucene - Indexing Process
- Lucene - Search Operation
- Lucene - Sorting
Lucene - Indexing Operations
- Lucene - Indexing Operations
- Lucene - Add Document
- Lucene - Update Document
- Lucene - Delete Document
- Lucene - Field Options
Lucene - Query Programming
- Lucene - Query Programming
- Lucene - TermQuery
- Lucene - TermRangeQuery
- Lucene - PrefixQuery
- Lucene - BooleanQuery
- Lucene - PhraseQuery
- Lucene - WildCardQuery
- Lucene - FuzzyQuery
- Lucene - MatchAllDocsQuery
- Lucene - MatchNoDocsQuery
- Lucene - RegexpQuery
Lucene - Analysis
- Lucene - Analysis
- Lucene - WhitespaceAnalyzer
- Lucene - SimpleAnalyzer
- Lucene - StopAnalyzer
- Lucene - StandardAnalyzer
- Lucene - KeywordAnalyzer
- Lucene - CustomAnalyzer
- Lucene - EnglishAnalyzer
- Lucene - FrenchAnalyzer
- Lucene - SpanishAnalyzer
Lucene - Resources
Lucene - FuzzyQuery
FuzzyQuery class is used to search documents using fuzzy implementation that is an approximate search based on the edit distance algorithm.
Class Declaration
Following is the declaration for org.apache.lucene.search.FuzzyQuery class −
public class FuzzyQuery extends MultiTermQuery
S.No. | Method & Description |
---|---|
1 |
static final int defaultMaxEdits
|
2 |
static final int defaultMaxExpansions
|
3 |
static final int defaultPrefixLength
|
4 |
static final boolean defaultTranspositions
|
S.No. | Constructor & Description |
---|---|
1 |
FuzzyQuery(Term term) Calls FuzzyQuery(term, defaultMaxEdits). |
2 |
FuzzyQuery(Term term, int maxEdits) Calls FuzzyQuery(term, maxEdits, defaultPrefixLength). |
3 |
FuzzyQuery(Term term, int maxEdits, int prefixLength) Calls FuzzyQuery(term, maxEdits, prefixLength, defaultMaxExpansions, defaultTranspositions). |
4 |
FuzzyQuery(Term term, int maxEdits, int prefixLength, int maxExpansions, boolean transpositions) Calls FuzzyQuery(Term, int, int, int, boolean, org.apache.lucene.search.MultiTermQuery.RewriteMethod) FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, defaultRewriteMethod(maxExpansions)) |
5 |
FuzzyQuery(Term term, int maxEdits, int prefixLength, int maxExpansions, boolean transpositions, MultiTermQuery.RewriteMethod rewriteMethod) Create a new FuzzyQuery that will match terms with an edit distance of at most maxEdits to term. |
S.No. | Method & Description |
---|---|
1 |
static MultiTermQuery.RewriteMethod defaultRewriteMethod(int maxExpansions) Creates a default top-terms blended frequency scoring rewrite with the given max expansions. |
2 |
boolean equals(Object obj) Override and implement query instance equivalence properly in a subclass. |
3 |
static int floatToEdits(float minimumSimilarity, int termLen) Helper function to convert from "minimumSimilarity" fractions to raw edit distances. |
4 |
CompiledAutomaton getAutomata() Returns the compiled automata used to match terms. |
5 |
static CompiledAutomaton getFuzzyAutomaton(String term, int maxEdits, int prefixLength, boolean transpositions) Returns the CompiledAutomaton internally used by FuzzyQuery to match terms. |
6 |
int getMaxEdits()
|
7 |
int getPrefixLength() Returns the non-fuzzy prefix length. |
8 |
Term getTerm() Returns the pattern term. |
9 |
protected TermsEnum getTermsEnum(Terms terms, AttributeSource atts) Construct the enumeration to be used, expanding the pattern term. |
10 |
boolean getTranspositions() Returns true if transpositions should be treated as a primitive edit operation. |
11 |
int hashCode() Override and implement query hash code properly in a subclass. |
12 |
String toString(String field) Prints a query to a string, with field assumed to be the default field and omitted. |
13 |
void visit(QueryVisitor visitor) Recurse through the query tree, visiting any child queries. |
Methods Inherited
This class inherits methods from the following classes −
- org.apache.lucene.search.MultiTermQuery
- org.apache.lucene.search.Query
- java.lang.Object
Usage of FuzzyQuery
private void searchUsingFuzzyQuery(String searchQuery) throws IOException, ParseException { searcher = new Searcher(indexDir); long startTime = System.currentTimeMillis(); //create a term to search file name Term term = new Term(LuceneConstants.FILE_NAME, searchQuery); //create the term query object Query query = new FuzzyQuery(term); //do the search TopDocs hits = searcher.search(query); long endTime = System.currentTimeMillis(); System.out.println(hits.totalHits + " documents found. Time :" + (endTime - startTime) + "ms"); for(ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = searcher.getDocument(scoreDoc); System.out.print("Score: "+ scoreDoc.score + " "); System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH)); } }
Example Application
To test search using BooleanQuery, let us create a test Lucene application.
Step | Description |
---|---|
1 | Create a project with a name LuceneFirstApplication under a package com.tutorialspoint.lucene as explained in the Lucene - First Application chapter. You can also use the project created in Lucene - First Application chapter as such for this chapter to understand the searching process. |
2 | Create LuceneConstants.java and Searcher.java as explained in the Lucene - First Application chapter. Keep the rest of the files unchanged. |
3 | Create LuceneTester.java as mentioned below. |
4 | Clean and Build the application to make sure business logic is working as per the requirements. |
LuceneConstants.java
This class is used to provide various constants to be used across the sample application.
package com.tutorialspoint.lucene; public class LuceneConstants { public static final String CONTENTS = "contents"; public static final String FILE_NAME = "filename"; public static final String FILE_PATH = "filepath"; public static final int MAX_SEARCH = 10; }
Searcher.java
This class is used to read the indexes made on raw data and searches data using the Lucene library.
package com.tutorialspoint.lucene; import java.io.IOException; import java.nio.file.Paths; import java.text.ParseException; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.QueryBuilder; public class Searcher { IndexSearcher indexSearcher; QueryBuilder queryBuilder; Query query; public Searcher(String indexDirectoryPath) throws IOException { DirectoryReader indexDirectory = DirectoryReader.open(FSDirectory.open(Paths.get(indexDirectoryPath))); indexSearcher = new IndexSearcher(indexDirectory); StandardAnalyzer analyzer = new StandardAnalyzer(); queryBuilder = new QueryBuilder(analyzer); } public TopDocs search( String searchQuery) throws IOException, ParseException { query = queryBuilder.createPhraseQuery(LuceneConstants.CONTENTS, searchQuery); return indexSearcher.search(query, LuceneConstants.MAX_SEARCH); } public TopDocs search(Query query) throws IOException, ParseException { return indexSearcher.search(query, LuceneConstants.MAX_SEARCH); } public Document getDocument(ScoreDoc scoreDoc) throws CorruptIndexException, IOException { return indexSearcher.storedFields().document(scoreDoc.doc); } }
LuceneTester.java
This class is used to test the searching capability of the Lucene library.
package com.tutorialspoint.lucene; import java.io.IOException; import java.text.ParseException; import org.apache.lucene.document.Document; import org.apache.lucene.index.Term; import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; public class LuceneTester { String indexDir = "D:\\Lucene\\Index"; String dataDir = "D:\\Lucene\\Data"; Searcher searcher; public static void main(String[] args) { LuceneTester tester; try { tester = new LuceneTester(); tester.searchUsingFuzzyQuery("cord3.txt"); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } private void searchUsingFuzzyQuery(String searchQuery) throws IOException, ParseException { searcher = new Searcher(indexDir); long startTime = System.currentTimeMillis(); //create a term to search file name Term term = new Term(LuceneConstants.FILE_NAME, searchQuery); //create the term query object Query query = new FuzzyQuery(term); //do the search TopDocs hits = searcher.search(query); long endTime = System.currentTimeMillis(); System.out.println(hits.totalHits + " documents found. Time :" + (endTime - startTime) + "ms"); for(ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = searcher.getDocument(scoreDoc); System.out.print("Score: "+ scoreDoc.score + " "); System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH)); } } }
Data & Index Directory Creation
I've used 10 text files from record1.txt to record10.txt containing names and other details of the students and put them in the directory D:\Lucene\Data. Test Data. An index directory path should be created as D:\Lucene\Index. After running the indexing program in the 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 your application runs successfully, it will print the following message in Eclipse IDE's console −
Output
1 hits documents found. Time :89ms Score: 0.73515606 File: D:\lucene\Data\record3.txt