• lucene-查询query->PhrasePrefixQuery使用短语缀搜索


    PhrasePrefixQuery与Phrase有些类似。在PhraseQuery中,如果用户想查找短语“david robert”,又想查找短语“mary robert”。那么,他就只能构建两个PhraseQuery,然后再使用BooleanQuery将它们作为其中的子句,并使用“或”操作符来连接,这样就能达到需要的效果。PhrasePrefixQuery可以让用户很方便地实现这种需要。

    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)方法设置出现在短语中的后一个词。

  • 相关阅读:
    在一个字符串中找到第一个只出现一次的字符
    声明数组变量/// 计算所有元素的总和/打印所有元素总和/输出/foreach循环/数组作为函数的参数/调用printArray方法打印
    intellij idea 如何更改编辑器文本字体和大小
    称砝码算法//输入与算法分开
    invalid types 'int[int]' for array subscript// EOF 输入多组数据//如何键盘输入EOF
    scanf和gets的差别
    输入3行字符串/定义flag/while/字符串后要加空格符
    ‘'的单引号/输入字符串/输出单个字符
    窗口迅速关闭的解决办法/scanf/if/for/break
    【笔记】【VSCode】Windows下VSCode编译调试c/c++
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5232442.html
Copyright © 2020-2023  润新知