Apache Lucene是一个由java编写的高性能,全方位的单词搜索引擎库。
Lucence可以分为两大块: 1,对文件建立索引;2,根据索引搜索文件。
一、建立索引的五个基础类 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 — 在内存中存储所有索引的实现。该类适用于较小的索引,可以完整加载到内存中,在应用 程序终止之后销毁。由于索引保存在内存中,所以速度相对较快。
二、搜索内容的四个基础类:Searche、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的简单用法。
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
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.RAMDirectory;
import org.apache.lucene.util.Version;
public class HelloWord {
public static RAMDirectory createIndexFile() {
IndexWriter indexWriter=null;
RAMDirectory directory = null;
try {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(
Version.LUCENE_40, analyzer);
//Directory directory = new SimpleFSDirectory(new File("./indexDir/"));
directory = new RAMDirectory();
indexWriter = new IndexWriter(directory,indexWriterConfig);
StringBuffer str = new StringBuffer();
for(int i=1;i<1000;i++){
str.append("x");
Article article = new Article(i,"中国第"+i+"大的城市",str.toString());
Document doc=new Document();
doc.add(new Field("id",article.getId().toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("title",article.getTitle().toString(),Store.YES,Index.ANALYZED));
doc.add(new Field("content",article.getContent().toString(),Store.YES,Index.ANALYZED));
indexWriter.addDocument(doc);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(indexWriter!=null){
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return directory;
}
//如果查询是需要用到解析器,那解析器必须和创建时的解析器相同
public static void searchIndexFileResult(Directory directory,String fieldName,String fieldValue) throws IOException {
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher searcher = new IndexSearcher(indexReader);
Query query = new TermQuery(new Term(fieldName,fieldValue));
TopDocs rs = searcher.search(query, null, 10);
Document firstHit = searcher.doc(rs.scoreDocs[0].doc);
System.out.println("数量:"+rs.totalHits);
System.out.println("id:"+firstHit.getField("id").stringValue());
System.out.println("title:"+firstHit.getField("title").stringValue());
}
public static void main(String[] args) throws IOException {
// 建立要索引的文件 ,从索引文件中查询数据
searchIndexFileResult(createIndexFile(),"content","xxxxxx");
/*
* 获得结果,然后交由相关应用程序处理
*
*/
}
}