FuzzyQuery fuzzy search
/*
* This fuzzy search method searches strings based on a single word entered by the user,
* This algorithm is called the levenshtein algorithm.
* When comparing two strings, this algorithm divides the action into three types,
* Add a letter, delete a letter, and change a letter. When comparing two strings
* The operation is to convert one string to another,
* If you fail to perform the preceding operation once, a certain score is deducted.
* When the comparison is complete, that is, the transformation is complete. The score is called the distance between the two.
* Also known as ambiguity.
**/
Package query;
Import java. io. IOException;
Import org. apache. lucene. analysis. standard. StandardAnalyzer;
Import org.apache.e.doc ument. Document;
Import org.apache.e.doc ument. Field;
Import org. apache. lucene. index. IndexWriter;
Import org. apache. lucene. index. Term;
Import org. apache. lucene. search. FuzzyQuery;
Import org. apache. lucene. search. Hits;
Import org. apache. lucene. search. IndexSearcher;
Import org. apache. lucene. search. MultiPhraseQuery;
Public class FuzzyQueryTest {
Public FuzzyQueryTest (String INDEX_STORE_PATH ){
Try {
IndexWriter writer = new IndexWriter (INDEX_STORE_PATH, new StandardAnalyzer (), true );
Writer. setUseCompoundFile (false );
// Create 3 Documents
Document doc1 = new Document ();
Document doc2 = new Document ();
Document doc3 = new Document ();
Field f1 = new Field ("content", "word", Field. Store. YES, Field. Index. TOKENIZED );
Field f2 = new Field ("content", "work", Field. Store. YES, Field. Index. TOKENIZED );
Field f3 = new Field ("content", "world", Field. Store. YES, Field. Index. TOKENIZED );
Doc1.add (f1 );
Doc2.add (f2 );
Doc3.add (f3 );
Writer. addDocument (doc1 );
Writer. addDocument (doc2 );
Writer. addDocument (doc3 );
Writer. close ();
IndexSearcher searcher = new IndexSearcher (INDEX_STORE_PATH );
// Construct a Term object and perform fuzzy search
Term t = new Term ("content", "work ");
FuzzyQuery query = new FuzzyQuery (t );
// Print the query Structure
Hits hits = searcher. search (query );
For (int I = 0; I System.out.println(hits.doc (I ));
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
Public static void main (String [] args ){
// TODO Auto-generated method stub
System. out. println ("about to perform fuzzy search --------------- >>>>>> ");
System. out. println ("fuzzy search in progress ");
FuzzyQueryTest fq = new FuzzyQueryTest ("E: \ Lucene project \ index file ");
System. out. println ("fuzzy search in progress ");
System. out. println ("query ended ----------------------- >>>>>>> ");
}
}