FuzzyQuery是一种模糊查询,它可以简单地识别两个相近的词语。下面以11.10为例进行详细介绍。
package ch11; 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; public class FuzzyQueryTest { public static void main(String[] args) throws Exception { //生成Document对象 Document doc1 = new Document(); //添加“content”字段的内容 doc1.add(Field.Text("content", "david")); //添加“title”字段的内容 doc1.add(Field.Keyword("title", "doc1")); Document doc2 = new Document(); doc2.add(Field.Text("content", "sdavid")); doc2.add(Field.Keyword("title", "doc2")); Document doc3 = new Document(); doc3.add(Field.Text("content", "davie")); doc3.add(Field.Keyword("title", "doc3")); //生成索引书写器 IndexWriter writer = new IndexWriter("c://index", new StandardAnalyzer(), true); //将文档添加到索引中 writer.addDocument(doc1); writer.addDocument(doc2); writer.addDocument(doc3); //关闭索引写器 writer.close(); //生成索引搜索器 IndexSearcher searcher = new IndexSearcher("c://index"); Term word1 = new Term("content", "david"); //用于保存检索结果 Hits hits = null; //生成FuzzyQuery对象,初始化为null FuzzyQuery query = null; query = new FuzzyQuery(word1); //开始检索,并返回检索结果 hits = searcher.search(query); //输出检索结果的相关信息 printResult(hits,"与'david'相似的词"); } public static void printResult(Hits hits, String key) throws Exception {System.out.println("查找 /"" + key + "/" :"); if (hits != null) { if (hits.length() == 0) { System.out.println("没有找到任何结果"); System.out.println(); } else { System.out.print("找到"); for (int i = 0; i < hits.length(); i++) { //取得文档对象 Document d = hits.doc(i); //取得“title”字段的内容 String dname = d.get("title"); System.out.print(dname + " "); } System.out.println(); System.out.println(); } } } }
在上述代码中,首先构建了3个Document,这3个Document的“content”字段中都有一个与“david”较为相似的关键字(其中第一个就是david)。然后使用FuzzyQuery来对其进行检索。