Lucene例子

Lucene的使用,简单来说可以划分为两种类型,一是建立索引,二是搜索内容。
一、建立索引的五个基础类 Document, Field, IndexWriter, Analyzer, Directory  
1、Document类:用来描述文档,这里的文档可以指一个HTML页面,一封电子邮件,或者是一个文本文件。一个 Document 对象由多个Field对象组成的。可以把一个Document对象想象成数据库中的一个记录,而每个Field对象就是记录的一个字段。
2、Field类:用来描述一个文档的某个属性,比如一封电子邮件的标题和内容可以用两个Field对象分别描述。
3、Analyzer类:用来对文档内容进行分词处理,Analyzer 类是一个抽象类,它有多个实现,针对不同的语言和应用需要选择适合的Analyzer,Analyzer会把分词后的内容交给IndexWriter来建立索引。
4、IndexWriter类:用来创建索引的一个核心类,作用是把一个个的Document对象加到索引中来。
5、Directory 类:表示索引文件存储位置的抽象类。有两个常用的子类:
    * FSDirectory — 在实际文件系统中存储索引的 Directory 实现。该类对于大型索引非常有用。
    * RAMDirectory — 在内存中存储所有索引的实现。该类适用于较小的索引,可以完整加载到内存中,在应用程序终止之后销毁。由于索引保存在内存中,所以速度相对较快。

二、搜索内容的四个基础类:Searcher、Term、Query和TopDocs     
1、Searcher 是一个抽象基类,包含各种超负荷搜索方法。IndexSearcher 是一个常用的子类,允许在给定的目录中存储搜索索引。Search 方法返回一个根据计算分数排序的文档集合。Lucene 为每个匹配给定查询的文档计算分数。IndexSearcher 是线程安全的;一个实例可以供多个线程并发使用。
2、Term 是搜索的基本单位。它由两部分组成:单词文本和出现该文本的字段的名称。Term 对象也涉及索引编制,但是可以在 Lucene 内部创建。
3、Query 和子类
Query 是一个用于查询的抽象基类。搜索指定单词或词组涉及到在项中包装它们,将项添加到查询对象,将查询对象传递到 IndexSearcher 的搜索方法。
Lucene 包含各种类型的具体查询实现,比如 TermQuery、BooleanQuery、PhraseQuery、PrefixQuery、RangeQuery、 MultiTermQuery、FilteredQuery、SpanQuery 等。以下部分讨论 Lucene 查询 API 的主查询类。
4、TopDocs    封装搜索结果以及 ScoreDoc 的总数

 

一个最简单的Lucene例子,展示Lucene中重要的几个类IndexWriter、Directory、Analyzer、Document、Field、IndexSearcher、Term、Query、TermQuery、TopDocs的简单用法。

package org.apache.lucene.demo;


import java.io.File;
import java.io.IOException;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;


import antlr.collections.List;


public class LuceneTest {
public static void main(String[] args) throws Exception {
Directory dir = FSDirectory.open(new File("d:/luence"));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,
analyzer);
iwc.setOpenMode(OpenMode.CREATE); // 即创建新索引文件OpenMode.CREATE_OR_APPEND表示创建或追加到已有索引文件
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new Field("partnum", "Q36", Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("description", "Illidium Space Modulator",
Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("partnum", "Q37", Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("description", "Illidium Space Modulator  1111",
Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
writer.close();
IndexSearcher searcher = new IndexSearcher(new SimpleFSDirectory(new File("d:/luence")));
Query query = new TermQuery(new Term("partnum", "Q36"));
TopDocs rs = searcher.search(query, 10);
for (int i = 0; i < rs.totalHits; i++) {
Document firstHit = searcher.doc(rs.scoreDocs[i].doc);
java.util.List<Fieldable> list = firstHit.getFields();
for (Fieldable fieldable : list) {
System.out.println(fieldable.name() + "----"
+ fieldable.stringValue());
}
}
}


}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值