基于Lucene2.4的全文检索分析(一)

本文介绍了一个使用Lucene实现的简单文档索引和搜索应用案例。通过创建两个包含标题和内容的文档,并利用StandardAnalyzer进行分析,演示了如何建立索引及执行查询。最后展示了查询结果及输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看下面的代码:

  1. package demo.basic;
  2. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  3. import org.apache.lucene.document.Document;
  4. import org.apache.lucene.document.Field;
  5. import org.apache.lucene.index.IndexWriter;
  6. import org.apache.lucene.queryParser.QueryParser;
  7. import org.apache.lucene.search.IndexSearcher;
  8. import org.apache.lucene.search.Query;
  9. import org.apache.lucene.search.ScoreDoc;
  10. import org.apache.lucene.search.TopDocs;
  11. import org.apache.lucene.store.RAMDirectory;
  12. public class LuceneTest {
  13.     public static void main(String[] args) throws Exception {
  14.         
  15.         RAMDirectory directory = new RAMDirectory();
  16.         IndexWriter.MaxFieldLength maxFieldLength = new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH);
  17.         IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), true, maxFieldLength);
  18.         
  19.         Document doc = new Document();
  20.         doc.add(new Field("title""php book", Field.Store.YES, Field.Index.ANALYZED));
  21.         doc.add(new Field("content""you can use this book to learn php.", Field.Store.YES, Field.Index.ANALYZED));
  22.         writer.addDocument(doc);
  23.         
  24.         doc = new Document();
  25.         doc.add(new Field("title""java book", Field.Store.YES, Field.Index.ANALYZED));
  26.         doc.add(new Field("content""this is the best book for learn java.", Field.Store.YES, Field.Index.ANALYZED));
  27.         writer.addDocument(doc);
  28.         
  29.         writer.close();
  30.         
  31.         IndexSearcher searcher = new IndexSearcher(directory);
  32.         QueryParser parser = new QueryParser("title"new StandardAnalyzer());
  33.         Query query = parser.parse("book");
  34.         TopDocs topDocs = searcher.search(query, 100);
  35.         ScoreDoc[] hits = topDocs.scoreDocs;
  36.         System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length + "条");
  37.         for (int i = 0; i < hits.length; i++) {
  38.             int DocId = hits[i].doc;
  39.             Document document = searcher.doc(DocId);
  40.             System.out.println(DocId + ":" + document.get("content"));
  41.         }
  42.         searcher.close();
  43.     }
  44. }

输出结果:

共有2条索引,命中2条
0:you can use this book to learn php.
1:this is the best book for learn java.

 

分析器StandardAnalyzer是lucene自带的。

上例中尝试了把2组数据放入索引中,每组数据有2个字段。这种结构和数据库表结构是非常相似的,使用时可以进行联想。

Field.Store.YES 表示:建立索引时该字段会被存储起来,待查询时可以通过 document.get 方法取出。

API说明:http://lucene.apache.org/java/2_4_0/api/core/index.html

Field.Index.ANALYZED 表示:建立索引时该字段会被指定的分析器进行分析。

API说明:http://lucene.apache.org/java/2_4_0/api/core/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值