FuzzyQuery fuzzy search

Source: Internet
Author: User

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 ----------------------- >>>>>>> ");
}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.