FuzzyQuery模糊搜索

//模糊搜索,可以进行单字查找
package query;
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.Hits;
import org.apache.lucene.search.IndexSearcher;


public class FuzzyQuery {


public static void main(String[] args) {
// TODO 自动生成的方法存根
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
//创建8个文档
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", "word",Field.Store.YES, Field.Index.TOKENIZED);
Field f3 = new Field("content", "word",Field.Store.YES, Field.Index.TOKENIZED);
Field f4 = new Field("content", "word",Field.Store.YES, Field.Index.TOKENIZED);
Field f5 = new Field("content", "word",Field.Store.YES, Field.Index.TOKENIZED);
Field f6 = new Field("content", "word",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);

//构建1个Term对象,然后对其进行模糊查找
Term t1 = new Term("contene","work");
FuzzyQuery query = new FuzzyQuery(t1);

//打印查询结果
Hits hits = searcher.search(query);
for(int i = 0; i < hits.length(); i++){
System.out.println(hits.doc(i));
}
}
}

你可能感兴趣的:(搜索引擎,Java)