Lucene 使用前缀检索Document中Field 以及关于 Hit

hits这个类相当于一个List<Document>,至少我是这么认为的。 可能当中的Doc已经被处理过了。

在高级的lucene jar版本中,可能被删除了这个类了。

Hits中的函数:

Method Summary
Document doc(intn)
Returns the stored fields of the nth document in this set.
int id(intn)
Returns the id for the nth document in this set.
int length()
Returns the total number of hits available in this set.
float score(intn)
Returns the score for the nth document in this set.

代码:

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; /** * 此次测试使用的是Lucene1.4.3 * @author 李晗 * */ public class Test { //http://lucene.apache.org/java/1_4_3/api/ public static void main(String[] args) throws Exception { // 生成Document对象,下同 Document doc1 = new Document(); // 添加new Fieldnamenew Field字段的内容,下同 //Field(String name, String string, boolean store, boolean index, boolean token) doc1.add(new Field("name", "lihan",true,true,true)); // 添加new Fieldtitlenew Field字段的内容,下同 doc1.add(new Field("title", "lihan", true,true,true)); Document doc2 = new Document(); doc2.add(new Field("name", "amen", true,true,true)); doc2.add(new Field("title", "amen", true,true,true)); Document doc3 = new Document(); doc3.add(new Field("name", "Wanglihong", true,true,true)); doc3.add(new Field("title", "shoushou", true,true,true)); Document doc4 = new Document(); doc4.add(new Field("name", "Zhoujielun", true,true,true)); doc4.add(new Field("title", "gongshang", true,true,true)); // 生成索引书写器 //IndexWriter(String path, Analyzer a, boolean create) Constructs an IndexWriter for the index in path. IndexWriter writer = new IndexWriter("d:\\index\\testPrefix", new StandardAnalyzer(), true); // 设置为混合索引模式 writer.setUseCompoundFile(true); // 依次将文档添加到索引中 writer.addDocument(doc1); writer.addDocument(doc2); writer.addDocument(doc3); writer.addDocument(doc4); // 关闭索引书写器 writer.close(); // 生成索引搜索器对象 IndexSearcher searcher = new IndexSearcher("d:\\index\\testPrefix"); // 构造词条 Term pre1 = new Term("name", "l"); Term pre2 = new Term("name", "zh"); Term pre3 = new Term("name", "a"); // 用于保存检索结果 Hits hits = null; // 生成PrefixQuery类型的对象,初始化为null PrefixQuery query = null; query = new PrefixQuery(pre1); // 开始第一次检索,并返回检索结果 hits = searcher.search(query); // 输出相应的检索结果 printResult(hits, "前缀为'l'的文档"); query = new PrefixQuery(pre2); // 开始第二次检索,并返回检索结果 hits = searcher.search(query); // 输出相应的检索结果 printResult(hits, "前缀为'zh'的文档"); query = new PrefixQuery(pre3); // 开始第二次检索,并返回检索结果 hits = searcher.search(query); // 输出相应的检索结果 printResult(hits, "前缀为'a'的文档"); } 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); // 取得new Fieldtitlenew Field字段的内容 String dname = d.get("title"); System.out.print(dname + " "); } System.out.println(); System.out.println(); } } } }

输出::

查找 "前缀为'l'的文档" :
找到lihan

查找 "前缀为'zh'的文档" :
找到gongshang

查找 "前缀为'a'的文档" :
找到amen

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值