Lucene是由Doug Cutting在1997年末开发的一款开源搜索引擎,并于2001年末赠与Apache开源组织,成为Apache的一个子项目。目前Lucene在开源社区中得到了大量开源支持者的赞同并一跃成为使用率最高的全文搜索引擎。截至目前为止,Lucene已经发展到了2.1版本。
Lucene不是一个完整的全文搜索应用,而是一款用Java写的全文搜索引擎工具包,它可以方便的嵌入到各种应用程序中实现针对应用的全文索引/检索功能。
全文检索
全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序根据事先建立的索引进行查找,并将查找到的结果反馈给用户的检索方式。
Lucene的包结构(org.apache.lucene)
analysis | 包括用于对需要建立索引的文本进行分词、过滤等操作,它还自带了支持德语和俄语的分词器和过滤器 |
standard | 由javaCC构建的Lucene标准分析器 |
Document | 提供对全文检索过程中的文件以及字段的抽象,并给出解决方案 |
Index | 对外提供建立索引时各种操作的支持 |
queryParser | 用于向Lucene提供检索时的分析支持 |
Search | 通过search包中提供的工具来完成对索引的检索 |
Store | 提供对索引存储的支持 |
util | 提供一些常用工具类和常量类的支持 |
Lucene的索引和检索
索引和检索是利用Lucene进行全文检索的两个主要步骤,具体的内容将在以后的日志中详细介绍,下面通过一个简单的例子来展示一下如何利用Lucene进行检索。
package testLucene;
import java.io.*;
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.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
public class LuceneTest {
public static void buildIndex() throws Exception {
Document doc1 = new Document();
doc1.add(new Field("contents","word1 word",Field.StorYES,Field.Index.TOKENIZED ));
e. doc1.add(new Field("path", "path//document1.txt",Field.Store.YES,Field.Index.NO ));
Document doc2 = new Document();
doc2.add(new Field("contents", "word2 word1",Field.Store.YES,Field.Index.TOKENIZED));
doc2.add(new Field("path", "path//document2.txt",Field.Store.YES,Field.Index.TOKENIZED));
IndexWriter writer = new IndexWriter("c://index",
new StandardAnalyzer(), true);
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.close();
}
public static void main(String[] args) throws Exception {
// 构建索引
buildIndex();
// 使用已经存在索引目录
Searcher searcher = new IndexSearcher("c://index");
// 使用标准分析器
Analyzer aStandardAnalyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("contents", aStandardAnalyzer);
String line = "word1";
Query query = parser.parse(line);
// 输出要搜索的内容
System.out.println("查找 : " + query.toString("contents"));
// 使用searcher对象的search方法进行搜索,返回的是一个Hits类型的对象
Hits hits = searcher.search(query);
// 使用Hits对象的length()方法,输出搜索到的文档的数量
System.out.println("总共找到 " + hits.length() + " 个文档");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println("=============================");
// 输出文档的ID编号
System.out.println("文档的内部ID号:" + hits.id(i));
// 输出文档的存放路径
String path = doc.get("path");
System.out.println("path:" + path);
String content = doc.get("contents");
System.out.println("contents:"+content);
}
searcher.close();
}
}
运行结果:
查找 : word1
总共找到 2 个文档
=============================
文档的内部ID号:0
path:path/document1.txt
contents:word1 word
=============================
文档的内部ID号:1
path:path/document2.txt
contents:word2 word1
从以上的程序可以看出,利用Lucene进行全文检索主要分为如下几个步骤:
1. 构建索引
l 对要构建的内容进行抽象,并设定出检索的字段名以及内容。这两项工作由Document以及Field来完成。
l 构建一个索引输出器,通过向输出器添加抽象后的Document进而来完成索引的生成。
2. 查询
l 对已经生成的索引文件生成一个索引搜索器用来完成对索引的查找工作。
l 使用语法分析器构建对索引字段的查询器,并与搜索关键字一起构建查询对象。
l 索引搜索器根据这个查询对象在索引中进行查找,并将结果返回到结果集Hits中。
3. 结果显示
Hits是一个结果集,其中包含了全文检索的所有结果。对hits进行循环并将每一条结果还原为一个document对象,document对象提供了对每个字段的显示功能。