【发布时间】:2016-09-26 11:06:44
【问题描述】:
每次添加文档后关闭 Lucene IndexWriter 会减慢我的索引过程吗?
我想,关闭和打开索引编写器会减慢我的索引过程,还是 Lucene 不是这样?
基本上,我在 Spring Batch 作业中有一个 Lucene 索引器步骤,并且我在 ItemProcessor 中创建索引。 Indexer Step 是一个分区步骤,我在创建 ItemProcessor 时创建 IndexWriter 并保持打开状态直到步骤完成。
@Bean
@StepScope
public ItemProcessor<InputVO,OutputVO> luceneIndexProcessor(@Value("#{stepExecutionContext[field1]}") String str) throws Exception{
boolean exists = IndexUtils.checkIndexDir(str);
String indexDir = IndexUtils.createAndGetIndexPath(str, exists);
IndexWriterUtils indexWriterUtils = new IndexWriterUtils(indexDir, exists);
IndexWriter indexWriter = indexWriterUtils.createIndexWriter();
return new LuceneIndexProcessor(indexWriter);
}
有没有办法在步骤完成后关闭这个IndexWriter?
另外,我遇到了问题,因为我也在这一步中搜索以查找重复的文档,但我通过在打开阅读器和搜索之前添加 writer.commit(); 解决了这个问题。
请建议我是否需要在每次添加文档后关闭并打开或可以一直保持打开?以及如何关闭StepExecutionListenerSupport的afterStep?
最初,我为每个文档关闭并重新打开,但索引过程非常缓慢,所以我认为这可能是原因。
【问题讨论】:
-
您应该绝对在整个索引过程中保持一个
IndexWriter打开。正如您已经看到的,为每个文档打开一个新文档会大大减慢它的速度。
标签: java lucene spring-batch