Lucene简介
lucene是apache旗下的一款高性能、可伸缩的开源的信息检索库,elasticsearch和sorl都是基于Lucene来实现的
概念
lucene中几个重要的概念
索引库Index
lucene中一个目录就是一个索引库,同一个文件夹下的所有文件构成一个索引库,一个索引是多个Document的集合
段Segment
Lucene索引可能由多个子索引组成,这些子索引称为段,每一段都是独立的索引,段与段之间是相互独立的。当添加一个新文档就会生成一个新的段,并且也会触发段文件合并,段文件中记录了索引中包含有多少个段,每个段包含有多少个文档
在Lucene中段是不可变的,只可读,不可写,此时对于不同的操作来说
- 增:有新数据需要创建索引时,由于段的不变性,会新建一个段来存储新增的数据
- 删:当删除数据时,由于段不可写,会在Lucene中新增一个.del文件,用来专门存储被删除的id。在查询时,被删除的数据还是可以被查到的,只有进行文档链表合并时,才会把删除的数据过滤掉(删除的数据在段合并的时候才会被真正移除)
- 改:修改操作其实就是删除+新增,先在.del文件中标记旧数据的删除,再在新段中添加一条更新后的数据
Lucene采用延迟写的方式,新增的数据会先写入内存,然后批量写到磁盘。若有一个段被写到磁盘,就会生成一个提交点,来记录所有提交后的段信息的文件。一个段一旦拥有了提交点,这个段就只可读不可写了,而段在内存中时,是只有写权限没有读权限的
延迟写可以减少磁盘写入次数,提升性能,降低磁盘压力
文档Document
文档(document),在lucene的定义中,文档是一系列域(field)的组合,而文档的域则代表一系列与文档相关的内容,与数据库表的记录的概念有点类似,一行记录所包含的字段对应的就是文档的域,举例来说,一个文档比如老师的个人信息,可能包括年龄、身高、性别、个人简介等等