错误有二:
其一:程序错误,ramwriter的关闭时机有问题,应在合并之前关闭。
FSDirectory fsDir = FSDirectory.getDirectory("/data/index", true);
RAMDirectory ramDir = new RAMDirectory();
IndexWriter fsWriter = new IndexWriter(fsDir, new StandardAnalyzer(), true);
IndexWriter ramWriter = new IndexWriter(ramDir, new StandardAnalyzer(), true);
while (there are documents to index)
{
... create Document ...
ramWriter.addDocument(doc);
if (condition for flushing memory to disk has been met)
{
fsWriter.addIndexes(new Directory[] { ramDir });
ramWriter.close();//这一行位置错误,应移到上一行的上面
ramWriter = new IndexWriter(ramDir, new StandardAnalyzer(), true);
}
}
其二:这样并不能提高性能,反而会更慢。
测试数据如下:
文件系统现有文件大小 内存索引大小 lucene版本 用时
767M <1k 2.2 272203 134250
770K 297 391
2.4-1.4G 2.4.1 164781 66938 67578
2.4版本的确比2.2的快一倍。
而如果直接写入文件系统,1.5G的现有索引文件,写入一条document只需要31ms。
本文指出在使用Lucene进行索引构建时常见的两个错误:一是ramwriter关闭时机不当,应在合并前关闭;二是认为该方法能提升性能但实际上可能适得其反。通过不同版本的性能对比,展示了直接写入文件系统的效率更高。
236

被折叠的 条评论
为什么被折叠?



