错误有二:
其一:程序错误,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。