Lucene全文检索系列(一)

1. Lucene简介

Lucene是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎。Lucene以其方便使用、快速实施以及灵活性受到普遍的关注。它能够方便地嵌入到各类应用中实现针对应用的全文索引、检索功能。数据库

 

2. lucene 的包结构

一、analysis对须要创建索引的文本进行分词、过滤等操做apache

二、standard是标准分析器数组

三、document提供对Document和Field的各类操做的支持。架构

四、index是最重要的包,用于向Lucene提供创建索引时各类操做的支持框架

五、queryParser提供检索时的分析支持函数

六、search负责检索工具

七、store提供对索引存储的支持优化

八、util提供一些经常使用工具类和常量类的支持对象

Lucene中的类主要组成以下:索引

1)org.apache.1ucene.analysis语言分析器,主要用于的切词Analyzer是一个抽象类,管理对文本内容的切分词规则。

2)org.apache.1uceene.document索引存储时的文档结构管理,相似于关系型数据库的表结构。

3)document包相对而言比较简单,该包下面有3个类,document相对于关系型数据库的记录对象,Field主要负责字段的管理。

4)org.apache.1ucene.index索引管理,包括索引创建、删除等。索引包是整个系统核心,全文检索的根本就是为每一个切出来的词建索引,查询时就只须要遍历索引,而不须要去正文中遍历,从而极大的提升检索效率。

5)org.apache.1ucene.queryParser查询分析器,实现查询关键词间的运算,如与、或、非等。

6)org.apache.1ucene.search检索管理,根据查询条件,检索获得结果。

7)org.apache.1ucene.store数据存储管理,主要包括一些底层的I/0操做。

8)org.apache.1ucene.util一些公用类。

 

3. Document文档

1)void add(Field field) 往Document对象中添加字段

2)void removeField(String name)删除字段。若多个字段以同一个字段名存在,则删除首先添加的字段;若不存在,则Document保持不变

3)void removeFields(String name)删除全部字段。若字段不存在,则Document保持不变

4)Field getField(String name)若多个字段以同一个字段名存在,则返回首先添加的字段;若字段不存在,则Document保持不变

5)Enumeration fields()返回Document对象的全部字段,以枚举类型返回

6)Field [] getFields(String name)根据名称获得一个Field的数组

7)String [] getValues(String name)根据名称获得一个Field的值的数组

Document doc1 = new Document();

doc1.add(new Field("name", "word1 word2 word3",Field.Store.NO,Field.Index.TOKENIZED));

Document doc2 = new Document();

doc2.add(new Field("name", "word1 word2 word3",Field.Store.NO,Field.Index.TOKENIZED));

4. Field字段

new  Field("name", "word1 word2 word3",Field.Store.YES,Field.Index.TOKENIZED)

(1)store类有3个公有的静态属性:

Store.NO:表示该Field不须要存储

Store.YES:表示该Field须要存储

Store.COMPRESS:表示用压缩方式来保存这个Field的值

(2)index类有4个公有的静态属性:

Index.NO:不须要索引

Index.TOKENIZED:先被分词再被索引

Index.UN_TOKENIZED:不对该Field进行分词,但会对它进行索引

Index.NO_NORMS:对该Field进行索引,可是不使用Analyzer,同时禁止它参加评分,主要是为了减小内存的消耗。

(3)Field类的构造方法

public  Field(String name,String value,Store store,Index index);//直接的字符串方式

public  Field(String name,String value,Store store,Index index,TermVector  termVector);

public  Field(String name,String value,Reader reader);//使用Reader从外部传入

public  Field(String name,String value,Reader reader,TermVector termVector);

public  Field(String name,byte[] value,Store store)//使用直接的二进制byte传入

当Field值为二进制时,可使用Lucene的压缩功能将其值进行压缩。

5.索引文件格式

1).fnm格式  包含了Document中全部field名称

2).fdt与.fdx格式  .fdt文件用于存储具备Store.YES属性的Field的数据;.fdx是一个索引,用于存储Document在.fdt中的位置。

3).tis 与.tii格式  .tis文件用于存储分词后的词条(Term),而.tii就是它的索引文件,它代表了每一个.tis文件中的词条的位置。

4)deletable格式 文档被删除后,会首先在deletable文件中留下一个记录,要真正删除时,才将索引除去。

5)复合索引格式 .cfs

使用IndexWriter的useCompoundFile()  默认为True

6.Directory索引的存放位置

a)FSDirectory.getDirectory(path, true)第二个参数表示删除掉目录内原有内容

IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);//删除原有索引

FSDirectory fsDir=FSDirectory.getDirectory(path,true);

IndexWriter writer = new IndexWriter(fsDir, new StandardAnalyzer(), true);

bRAMDirectory在内存中存放,读取速度快,但程序一运行结束,它的内容就不存在了

RAMDirectory ramDir=new RAMDirectory();

IndexWriter writer = new IndexWriter(ramDir, new StandardAnalyzer(), true);

IndexWriter writer = new IndexWriter(new RAMDirectory(), new StandardAnalyzer(), true);

7.优化索引

writer.optimize();

将磁盘上的多个segment进行合并,组成一个全新的segment。这种方法并不会增长建索时的速度,反而会下降建索的速度。因此应该在建完索引后在调用这个函数

8.索引的合并

RAMDirectory RAMDir=new RAMDirectory();

IndexWriter writer = new IndexWriter(RAMDir, new StandardAnalyzer(), true);//删除原有索引

IndexWriter writer2=new IndexWriter(FSDirectory.getDirectory(path,true),new StandardAnalyzer(), true); 

writer.addDocument(doc1);

writer2.addDocument(doc2);

writer.close();

writer2.addIndexes(new Directory[]{RAMDir});

writer2.close();

注意:在合并前必定要先关闭要加的索引器。

9.Lucene的索引“锁”

1.  write.lock

2.  commit.lock

 

更多精彩文章请关注 =》 我爱学框架

http://www.itframe.top/