Lucene也提供了通配符的查询,这就是WildcardQuery。
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.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.WildcardQuery; public class WildcardQueryTest { public static void main(String[] args) throws Exception { //生成Document对象,下同 Document doc1 = new Document(); //添加“content”字段的内容,下同 doc1.add(Field.Text("content", "whatever")); //添加“title”字段的内容,下同 doc1.add(Field.Keyword("title", "doc1")); Document doc2 = new Document(); doc2.add(Field.Text("content", "whoever")); doc2.add(Field.Keyword("title", "doc2")); Document doc3 = new Document(); doc3.add(Field.Text("content", "however")); doc3.add(Field.Keyword("title", "doc3")); Document doc4 = new Document(); doc4.add(Field.Text("content", "everest")); doc4.add(Field.Keyword("title", "doc4")); //生成索引书写器 IndexWriter writer = new IndexWriter("c://index", new StandardAnalyzer(), true); //将文档对象添加到索引中 writer.addDocument(doc1); writer.addDocument(doc2); writer.addDocument(doc3); writer.addDocument(doc4); //关闭索引书写器 writer.close(); //生成索引书写器 IndexSearcher searcher = new IndexSearcher("c://index"); //构造词条 Term word1 = new Term("content", "*ever"); Term word2 = new Term("content", "wh?ever"); Term word3 = new Term("content", "h??ever"); Term word4 = new Term("content", "ever*"); //生成WildcardQuery对象,初始化为null WildcardQuery query = null; //用于保存检索结果 Hits hits = null; query = new WildcardQuery(word1); //开始第一次检索,并返回检索结果 hits = searcher.search(query); //输出检索结果的相关信息 printResult(hits, "*ever"); query = new WildcardQuery(word2); //开始第二次检索,并返回检索结果 hits = searcher.search(query); //输出检索结果的相关信息 printResult(hits, "wh?ever"); query = new WildcardQuery(word3); //开始第三次检索,并返回检索结果 hits = searcher.search(query); //输出检索结果的相关信息 printResult(hits, "h??ever"); query = new WildcardQuery(word4); //开始第四次检索,并返回检索结果 hits = searcher.search(query); //输出检索结果的相关信息 printResult(hits, "ever*"); } 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(); } } } }
由上述代码可以看出,通配符“?”代表1个字符,而“*”则代表0至多个字符。不过通配符检索和上面的FuzzyQuery由于需要对字段关键字进行字符串匹配,所以,在搜索的性能上面会受到一些影响。
时间: 2024-10-21 22:46:33