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”字段中关键字的前缀。

 

转载于:https://www.cnblogs.com/hwaggLee/p/5232421.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值