lucene-查询query->PhrasePrefixQuery使用短语缀搜索
2016-03-01 20:36
465 查看
PhrasePrefixQuery与Phrase有些类似。在PhraseQuery中,如果用户想查找短语“david robert”,又想查找短语“mary robert”。那么,他就只能构建两个PhraseQuery,然后再使用BooleanQuery将它们作为其中的子句,并使用“或”操作符来连接,这样就能达到需要的效果。PhrasePrefixQuery可以让用户很方便地实现这种需要。
在上述代码中,首先构建了一个Document,它的“content”字段中包含4个关键字。接下来,构建了一个PhrasePrefixQuery的对象,调用它的add(Term [])方法设定出现在短语中的第一个关键词。由于这个方法的参数类型为一个Term型的数组,所以,它可以设置多个Term,即出现在短语中的第一个词就在这个数组中进行选择。然后,再使用add(Term)方法设置出现在短语中的后一个词。
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.PhrasePrefixQuery; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.RangeQuery; public class PhrasePrefixQueryTest { public static void main(String[] args) throws Exception { //生成Document对象 Document doc1 = new Document(); //添加“content”字段的内容 doc1.add(Field.Text("content", "david mary smith robert")); //添加“title”字段的内容 doc1.add(Field.Keyword("title", "doc1")); //生成索引书写器对象 IndexWriter writer = new IndexWriter("c://index", new StandardAnalyzer(), true); //将文档添加到索引中 writer.addDocument(doc1); //关闭索引书写器 writer.close(); //生成索引检索器 IndexSearcher searcher = new IndexSearcher("c://index"); //构造词条 Term word1 = new Term("content", "david"); Term word2 = new Term("content", "mary"); Term word3 = new Term("content", "smith"); Term word4 = new Term("content", "robert"); //用于保存检索结果 Hits hits = null; //生成PhrasePrefixQuery对象,初始化为null PhrasePrefixQuery query = null; query = new PhrasePrefixQuery(); // 加入可能的所有不确定的词 query.add(new Term[]{word1, word2}); // 加入确定的词 query.add(word4); //设置坡度 query.setSlop(2); //开始检索,并返回检索结果 hits = searcher.search(query); //输出检索结果的相关信息 printResult(hits, "存在短语'david robert'或'mary robert'的文档"); } 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(); } } } }
在上述代码中,首先构建了一个Document,它的“content”字段中包含4个关键字。接下来,构建了一个PhrasePrefixQuery的对象,调用它的add(Term [])方法设定出现在短语中的第一个关键词。由于这个方法的参数类型为一个Term型的数组,所以,它可以设置多个Term,即出现在短语中的第一个词就在这个数组中进行选择。然后,再使用add(Term)方法设置出现在短语中的后一个词。
相关文章推荐
- UIView设置背景图片
- lucene-查询query->PhraseQuery多关键字的搜索
- Foundation-NSOperation-NSInvocationOperation/NSBlockOperation/NSOperationQueue
- POJ 2778 DNA Sequence 题解&代码
- lucene-查询query->PrefixQuery使用前缀搜索
- lucene-查询query->RangeQuery在某一范围内搜索
- lucene-查询query->BooleanQuery “与或”搜索
- lucene-查询query->TermQuery按词条搜索
- 瀑布流UICollectionView
- UITableView总忘记的
- Error instantiating class book.pojo.User with invalid types () or values (). 报错解决方法
- StringBuffer StringBuilder线程安全的理解
- phpcms V9.6.0版本整合百度ueditor1.4.3.2,包括水图片上传水印
- UIScrollView的监听方法和定时器的使用
- android 解决事件冲突 requestDisallowInterceptTouchEvent 分析
- UI基础控件
- 二、LinkedQueue、LinkedStack
- SPOJ 16580 QTREE7 - Query on a tree VII
- UIKit-UIBezierPath
- easyui tree loadFilter的使用