分享

Lucence的结构 - neuron - 计世博客

 软件团队头目 2006-12-21

本文主要讨论Lucene的系统结构,希望对其结构的初步分析,更深入的了解Lucene的运作机制,从而实现对Lucene的功能扩展。

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->1. <!--[endif]-->Lucene的包结构

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !vml]--><!--[endif]-->

如上图所示,Lucene源码中共包括7个子包,每个包完成特定的功能:

<!--[if !supportEmptyParas]--> <!--[endif]-->

Lucene包结构功能表

包名

功能

org.apache.lucene.analysis

语言分析器,主要用于的切词,支持中文主要是扩展此类

org.apache.lucene.document

索引存储时的文档结构管理,类似于关系型数据库的表结构

org.apache.lucene.index

索引管理,包括索引建立、删除等

org.apache.lucene.queryParser

查询分析器,实现查询关键词间的运算,如与、或、非等

org.apache.lucene.search

检索管理,根据查询条件,检索得到结果

org.apache.lucene.store

数据存储管理,主要包括一些底层的I/O操作

org.apache.lucene.util

一些公用类

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->2. <!--[endif]-->Lucene的主要逻辑图

<!--[if !supportEmptyParas]--> <!--[endif]-->

Lucene功能强大,但从根本上说,主要包括两块:一是文本内容经切词后索引入库;二是根据查询条件返回结果。

以下是上述两大功能的逻辑图:

<!--[if !supportEmptyParas]--> <!--[endif]-->

STORAGE

(存储器)

ACCESS INDEX

(访问索引)

SERACHER

(查询器)

ANALYZER

(语言分析器)

QUERY PARSER

(查询分析器)

DOCUMENT

(文档结构)

SEARCHER

(查询)

INDEXER

(入库)

 

FS

 

BDD

 

RAM

Lucene功能逻辑图

<!--[if !vml]-->
<!--[endif]--><!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->


查询逻辑

<!--[if !supportEmptyParas]--> <!--[endif]-->

按先后顺序,查询逻辑可分为如下几步:

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->1.  <!--[endif]-->查询者输入查询条件
条件之间可以通过特定运算符进行运算,比如查询希望查询到与“中国”和“北京”相关的记录,但不希望结果中包括“海淀区中关村”,于是输入条件为“中国+北京-海淀区中关村”;

<!--[if !supportLists]-->2.  <!--[endif]-->查询条件被传达到查询分析器中,分析器将将对“中国+北京-海淀区中关村”进行分析,首先分析器解析字符串的连接符,即这里的加号和减号,然后对每个词进行切词,一般最小的词元是两个汉字,则中国和北京两个词不必再切分,但对海淀区中关村需要切分,假设根据切词算法,把该词切分为“海淀区”和“中关村”两部分,则最后得到的查询条件可以表示为:“中国” AND “北京” AND NOT(“海淀区” AND “中关村”)

<!--[if !supportLists]-->3.  <!--[endif]-->查询器根据这个条件遍历索引树,得到查询结果,并返回结果集,返回的结果集类似于JDBC中的ResultSet

<!--[if !supportLists]-->4.  <!--[endif]-->将返回的结果集显示在查询结果页面,当点击某一条内容时,可以链接到原始网页,也可以打开全文检索库中存储的网页内容。

<!--[if !supportEmptyParas]--> <!--[endif]-->

这就是查询的逻辑过程,需要说明的是,Lucene默认只支持英文,为了便于说明问题,以上查询过程采用中文举例,事实上,当Lucene被扩充支持中文后就是这么一个查询过程。

<!--[if !supportEmptyParas]--> <!--[endif]-->

入库逻辑

<!--[if !supportEmptyParas]--> <!--[endif]-->

入库将把内容加载到全文检索库中,按顺序,入库逻辑包括如下过程:

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->1.  <!--[endif]-->入库者定义到库中文档的结构,比如需要把网站内容加载到全文检索库,让用户通过“站内检索”搜索到相关的网页内容。入库文档结构与关系型数据库中的表结构类似,每个入库的文档由多个字段构成,假设这里需要入库的网站内容包括如下字段:文章标题、作者、发布时间、原文链接、正文内容(一般作为网页快照)。

<!--[if !supportLists]-->2.  <!--[endif]-->包含N个字段的文档(DOCUMENT)在真正入库前需要经过切词(或分词)索引,切词的规则由语言分析器(ANALYZER)完成。

<!--[if !supportLists]-->3.  <!--[endif]-->切分后的“单词”被注册到索引树上,供查询时用,另外也需要也其它不需要索引的内容入库,所有这些是文件操作均由STORAGE完成。

<!--[if !supportEmptyParas]--> <!--[endif]-->

以上就是记录加载流程,索引树是一种比较复杂的数据存储结构,将在后续章节陆续介绍,这里就不赘述了,需要说明的一点是,Lucene的索引树结构非常优秀,是Lucene的一大特色。

<!--[if !supportEmptyParas]--> <!--[endif]-->

接下来将对Lucene的各个子包的结构进行讨论。

 

<!--[if !supportLists]-->3. <!--[endif]-->语言分析包org.apache.lucene.analysis

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

Analyzer是一个抽象类,司职对文本内容的切分词规则。

<!--[if !supportEmptyParas]--> <!--[endif]-->

切分后返回一个TokenStreamTokenStream中有一个非常重要方法next(),即取到下一个词。简单点说,通过切词规则,把一篇文章从头到尾分成一个个的词,这就是org.apache.lucene.analysis的工作。

<!--[if !supportEmptyParas]--> <!--[endif]-->

对英文而言,其分词规则很简单,因为每个单词间都有一个空格,按空格取单词即可,当然为了提高英文检索的准确度,也可以把一些短语作为一个整体,其间不切分,这就需要一个词库,对德文、俄文也是类似,稍有不同。

<!--[if !supportEmptyParas]--> <!--[endif]-->

对中文而言,文字之间都是相连的,没有空格,但我们同样可以把字切分,即把每个汉字作为一个词切分,这就是所谓的“切字”,但切字方式方式的索引没有意义,准确率太低,要想提高准确度一般都是切词,这就需要一个词库,词库越大准确度将越高,但入库效率越低。

<!--[if !supportEmptyParas]--> <!--[endif]-->

若要支持中文切词,则需要扩展Analyzer类,根据词库中的词把文章切分。

<!--[if !supportEmptyParas]--> <!--[endif]-->

简单点说,org.apache.lucene.analysis就是完成将文章切分词的任务。

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->4. <!--[endif]-->文档结构包org.apache.lucene.document

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

document包相对而言比较简单,该包下面就3个类,Document相对于关系型数据库的记录对象,主要负责字段的管理,字段分两种,一是Field,即文本型字段,另一个是日期型字段DateField。这个包中关键需要理解的是Field中字段存储方式的不同,这在上一篇中已列表提到,下面我们可以参见一下其详细的类图:

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->5. <!--[endif]-->索引管理包org.apache.lucene.index

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

索引包是整个系统核心,全文检索的的根本就为每个切出来的词建索引,查询时就只需要遍历索引,而不需要去正文中遍历,从而极大的提高检索效率,索引建设的质量关键整个系统的质量。Lucene的索引树是非常优质高效的,具体的索引树细节,将在后续章节中重要探讨。

<!--[if !supportEmptyParas]--> <!--[endif]-->

在这个包中,主要学习IndexWriterIndexReader这个类。

<!--[if !supportEmptyParas]--> <!--[endif]-->

通过上一篇的初步应用可知,全文检索库的初始化和记录加载均需要通过该类来完成。

<!--[if !supportEmptyParas]--> <!--[endif]-->

初始化全文库的语句为:

IndexWriter indexWriter = new IndexWriter(“全文库的目录位置”,new StandardAnalyzer(),true);

<!--[if !supportEmptyParas]--> <!--[endif]-->

记录加载的语句为:indexWriter.addDocument(doc);

<!--[if !supportEmptyParas]--> <!--[endif]-->

IndexWriter主要用于写库,当需要读取库内容时,就需要用到IndexReader这个类了。

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->6. <!--[endif]-->查询分析包org.apache.lucene.queryParser和检索包org.apache.lucene.search
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

通过查询分析器(queryParser)解析后,将返回一个查询对象(query),根据查询对象就可进行检索了。上图描述了query对象的生成,下图描述了查询结果集(Hits)的生成。

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->7. <!--[endif]-->存储包org.apache.lucene.store

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

一些底层的文件I/O操作。

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->8. <!--[endif]-->工具包org.apache.lucene.util

<!--[if !vml]--><!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

该包中包括4个工具类。

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->9. <!--[endif]-->总结

<!--[if !supportEmptyParas]--> <!--[endif]-->

通过对Lucene源码包的分析,我们可以初步认识到Lucene的核心类包主要有3个:

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportLists]-->l         <!--[endif]-->org.apache.lucene.analysis

<!--[if !supportLists]-->l         <!--[endif]-->org.apache.lucene.index

<!--[if !supportLists]-->l         <!--[endif]-->org.apache.lucene.search

<!--[if !supportEmptyParas]--> <!--[endif]-->

其中org.apache.lucene.analysis 主要用于切分词,切分词的工作由Analyzer的扩展类来实现,Lucene自带了StandardAnalyzer类,我们可以参照该写出自己的切词分析器类,如中文分析器等。

<!--[if !supportEmptyParas]--> <!--[endif]-->

org.apache.lucene.index主要提供库的读写接口,通过该包可以创建库、添加删除记录及读取记录等。

<!--[if !supportEmptyParas]--> <!--[endif]-->

org.apache.lucene.search主要提供了检索接口,通过该包,我们可以输入条件,得到查询结果集,与org.apache.lucene.queryParser包配合还可以自定义的查询规则,像google一样支持查询条件间的与、或、非、属于等复合查询。


本文引用通告地址(TrackBack Ping URL)为:

http://blog./trackback.jsp?postID=8091

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多