Lucene fuzzy query
Jimmy
posted @ 2011年3月11日 20:40
in Others
, 2097 阅读
使用Levenshetein distance
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
String INDEX_STORE_PATH = "E://fuzzyQueryIndexFile";
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));
}
}