Lucene fuzzy query
Jimmy
posted @ 2011年3月11日 20:40
in Others
, 2107 阅读
使用Levenshetein distance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <span style= "font-size:16px;" > 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.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 java.io.*; public class fuzzyquery { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true ); writer.setUseCompoundFile( false ); Document doc1 = new Document(); Document doc2 = new Document(); Document doc3 = new Document(); Document doc4 = new Document(); Document doc5 = new Document(); Document doc6 = 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); Field f4 = new Field( "content" , "seed" , Field.Store.YES, Field.Index.TOKENIZED); Field f5 = new Field( "content" , "sword" , Field.Store.YES, Field.Index.TOKENIZED); Field f6 = new Field( "content" , "ford" , Field.Store.YES, Field.Index.TOKENIZED); doc1.add(f1); doc2.add(f2); doc3.add(f3); doc4.add(f4); doc5.add(f5); doc6.add(f6); writer.addDocument(doc1); writer.addDocument(doc2); writer.addDocument(doc3); writer.addDocument(doc4); writer.addDocument(doc5); writer.addDocument(doc6); writer.close(); IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH); Term t = new Term( "content" , "work" ); FuzzyQuery query = new FuzzyQuery(t); Hits hits = searcher.search(query); for ( int i= 0 ; i<hits.length();i++) System.out.println(hits.doc(i)); } }</span> |