ChristianK

笨鸟先飞,笨猪先肥

公告

统计

2010年5月26日

关于Lucene实时更新的尝试(续一)

 

先明确一下各种格式文件的概念

 

Name

Extension

Brief Description

Segments File

segments.gen, segments_N

Stores information about segments

Compound File

.cfs

An optional "virtual" file consisting of all the other index files for systems that frequently run out of file handles.

Compound Files

Starting with Lucene 1.4 the compound file format became default. This is simply a container for all files described in the next section (except for the .del file).

Compound (.cfs) --> FileCount, <DataOffset, FileName> FileCount , FileData FileCount

FileCount --> VInt

DataOffset --> Long

FileName --> String

FileData --> raw file data

The raw file data is the data from the individual files named above.

Starting with Lucene 2.3, doc store files (stored field values and term vectors) can be shared in a single set of files for more than one segment. When compound file is enabled, these shared files will be added into a single compound file (same format as above) but with the extension .cfx.

 

索引在创建时会有很多临时文件,如fdt,fdx,fdm等(具体参见《Apache Lucene - Index File Formats http://lucene.apache.org/java/2_3_2/fileformats.html ),上述文件共同组成索引片段SegmentIndexWriter工作时,会合并多个Segment文件到一个文件中,这个文件就是.cfs文件。合并Segments的好处是减少索引文件的数量,也就减少了检索过程中需要打开的文件的数量(据说有的系统会限制打开文件的数量,“据说而已,没有亲见”,但是想象中打开文件太多也不是好事,毕竟占用的句柄在哪摆着呢)。

 IndexWritersetUseCompoundFiletrue)可以控制是否创建复合文件,有一些参数来控制cfs文件的创建,CompoundFileReader也可以查看cfs文件,apache上很容易找到相关信息,这里都暂不赘述。

 

从上述描述还可以看出,从2.3版本开始,存储field valuesterm vectors的文档存储文件被存储为一个单独的复合文件,以被多个segment共享,这个文件格式不变,但是扩展名变成了.cfx。

 

可以得到一个结论就是:索引片段越趋于松散,检索的效率将越低。

 

实时更新的前提是维持检索本身的效率,而影响检索的效率的因素又包括cfs文件的数量,同时仅仅更新索引却不Optimize却势必要导致新的零散的cfs文件的不断出现,这样得就要明确一个参数,那就是新索引出现的频率是多少。假设1天1w条新数据要补充进,那么三个月就是90w条,如果每1000个文档生成一个cfs文件,那么三个月后就将累积900个索引碎片。

 

应该以多大的频率来Optimize索引?目前手头没有合适的样例数据,晚上回去测试一下,得到一个可视化的数据,先来佐证一下索引片段过多会对效率有多大的损耗再说吧。

 

to be continue...

 

 

posted @ 2010-05-26 13:57 闻滨 阅读(545) 评论(0) 编辑

2010年5月21日

尝试控制Lucene索引编辑过程中的CPU占用

  使用Lucene过程中一直以来比较痛苦的一件事就是在它执行Optimize方法是会霸占系统大量资源(说“独占”都不为过),一个29万条数据,4G+的索引,在我的笔记本上Optimize的时间长达32分钟,这期间我几乎只能去台式机上干活。很遗憾的是起码它开放的API我还没找到控制CPU占用的方法,事实上,想象中也比较困难,因为写入磁盘IO的事情,可是计算的动作就是要消耗CPU。最终在一个外国人的邮件回复中得到了一个结论,那就是“不能控制CPU资源的占用”:

I think I answered that question just the other day.... privately...
No, there is nothing in Lucene to help you with CPU utilization.
However, if you are running this on a UNIX box of some kind, you can (re)nice the process and thus lower its priority, giving other processes more time with the CPU. Windows may have something similar.

 

  暂时只能这样了,可是如果Lucene索引维护的进程和网站的应用进程在同一个服务器上,维护Lucene索引肯定会对网站正常的应用产生影响,恐怕这也是为什么很多人设计方案时将维护索引的时间设定在凌晨两点到四点之间的原因了吧。

posted @ 2010-05-21 12:59 闻滨 阅读(129) 评论(0) 编辑
Lucene.net索引实时更新

在Apache的更新日志上看到这样一句话

 

25 September 2009 - Lucene Java 2.9.0 available

This release has many improvements since release 2.4.1, including:

  • Near real-time search capabilities added to IndexWriter

Oops……这倒是个好消息,用上次备份的4G+的索引测试了一下,基本上可以称为“实时更新”了。不过还是那个疑问:索引碎片过多,会不会造成搜索效率的降低?

to be continue……

posted @ 2010-05-21 12:37 闻滨 阅读(491) 评论(0) 编辑
换汤不换药的UpdateDocuments

 

一直很好奇当初Apache曾经说的即将推出的UpdateDocuments()方法是个什么样的方法,Lucene索引的这种紧凑的文件结构如何能“允许”这个“Update”动作的发生呢?

千呼万唤始出来,不出所料,还是换汤不换药

 

注释
    /// In either case, documents are added with {@link #AddDocument(Document)
    
/// addDocument} and removed with {@link #DeleteDocuments(Term)} or {@link
    
/// #DeleteDocuments(Query)}. A document can be updated with {@link
    
/// #UpdateDocument(Term, Document) updateDocument} (which just deletes
    
/// and then adds the entire document). When finished adding, deleting 
    
/// and updating documents, {@link #Close() close} should be called.

 

 

 

posted @ 2010-05-21 12:19 闻滨 阅读(122) 评论(0) 编辑

2010年5月4日

被win7的管理员权限折腾的……

因故要注册iis,结果去运行aspnet_regiis -i老是报一个莫名其妙的0x800702e4 请求的操作需要提升的”错误,强烈怀疑我是不是少了什么补丁,原来只要把Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe创建一个快捷方式,给它的目标后面加上一个-i,管理员权限运行这个快捷方式就行,又是管理员权限,哎

posted @ 2010-05-04 14:17 闻滨 阅读(272) 评论(1) 编辑

2009年8月8日

被Jquery的Intellisense玩死了

该装的补丁都装完了,Ctrl+Shift+J,始终报错,百思不得其解。

 

网络又断了,百无聊赖,看那个Jquery 1.3.2-vsdoc2.js的“2”十分别扭,干脆给删掉了

 

再试,成了……

 

我靠,不是这么玩我吧?

posted @ 2009-08-08 16:31 闻滨 阅读(29) 评论(0) 编辑