zoukankan      html  css  js  c++  java
  • lucene-查询query->PrefixQuery使用前缀搜索

    PrefixQuery就是使用前缀来进行查找的。通常情况下,首先定义一个词条Term。该词条包含要查找的字段名以及关键字的前缀,然后通过该词条构造一个PrefixQuery对象,就可以进行前缀查找了。

    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.PrefixQuery;
    
    import org.apache.lucene.search.RangeQuery;
    
     
    
    public class PrefixQueryTest {
    
         public static void main(String[] args) throws Exception {
    
             //生成Document对象,下同
    
             Document doc1 = new Document();
    
             //添加“name”字段的内容,下同
    
             doc1.add(Field.Text("name", "David"));
    
             //添加“title”字段的内容,下同
    
             doc1.add(Field.Keyword("title", "doc1"));
    
     
    
             Document doc2 = new Document();
    
             doc2.add(Field.Text("name", "Darwen"));
    
             doc2.add(Field.Keyword("title", "doc2"));
    
     
    
             Document doc3 = new Document();
    
             doc3.add(Field.Text("name", "Smith"));
    
             doc3.add(Field.Keyword("title", "doc3"));
    
     
    
             Document doc4 = new Document();
    
             doc4.add(Field.Text("name", "Smart"));
    
             doc4.add(Field.Keyword("title", "doc4"));
    
     
    
             //生成索引书写器
    
             IndexWriter writer = new IndexWriter("c://index",
    
                     new StandardAnalyzer(), true);
    
             //设置为混合索引模式
    
             writer.setUseCompoundFile(true);
    
             //依次将文档添加到索引中
    
             writer.addDocument(doc1);
    
             writer.addDocument(doc2);
    
             writer.addDocument(doc3);
    
             writer.addDocument(doc4);
    
             //关闭索引书写器
    
             writer.close();
    
     
    
             //生成索引搜索器对象
    
             IndexSearcher searcher = new IndexSearcher("c://index");
    
             //构造词条
    
             Term pre1 = new Term("name", "Da");
    
             Term pre2 = new Term("name", "da");
    
             Term pre3 = new Term("name", "sm");
    
     
    
             //用于保存检索结果
    
             Hits hits = null;
    
             //生成PrefixQuery类型的对象,初始化为null
    
             PrefixQuery query = null;
    
     
    
             query = new PrefixQuery(pre1);
    
     
    
             //开始第一次检索,并返回检索结果
    
             hits = searcher.search(query);
    
             //输出相应的检索结果
    
             printResult(hits, "前缀为'Da'的文档");
    
            
    
             query = new PrefixQuery(pre2);
    
             //开始第二次检索,并返回检索结果
    
             hits = searcher.search(query);
    
             //输出相应的检索结果
    
             printResult(hits, "前缀为'da'的文档");
    
            
    
             query = new PrefixQuery(pre3);
    
             //开始第二次检索,并返回检索结果
    
             hits = searcher.search(query);
    
             //输出相应的检索结果
    
             printResult(hits, "前缀为'sm'的文档");
    
     
    
         }
    
     
    
         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();
    
                 }
    
             }
    
         }
    
    }

    代码中,首先构造了4个不同的Document。每个Document都有一个名为“name”的字段,其中存储了人物的名称。然后,代码构建了3个不同的词条,分别为“Da”、“da”和“sm”,可以看到,它们正好都是“name”字段中关键字的前缀。

  • 相关阅读:
    Ubuntu 14.04 卸载通过源码安装的库
    Ubuntu 14.04 indigo 相关依赖
    Ubuntu 14.04 indigo 安装 cartographer 1.0.0
    Ubuntu 14.04 改变文件或者文件夹的拥有者
    安装cartographer遇到Unrecognized syntax identifier "proto3". This parser only recognizes "proto2"问题
    Unrecognized syntax identifier "proto3". This parser only recognizes "proto2". ”问题解决方法
    查看所有用户组,用户名
    1卸载ROS
    Ubuntu14.04 软件安装卸载
    Ubuntu14.04系统显示器不自动休眠修改
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/5232421.html
Copyright © 2011-2022 走看看