Is closing Lucene IndexWriter
after each document addition slow down my indexing process?
I imagine, closing and opening index writer will slow down my indexing process or is it not true for Lucene?
Basically, I have a Lucene Indexer Step in a Spring Batch Job and I am creating indices in ItemProcessor
. Indexer Step is a partitioned step and I create IndexWriter
when ItemProcessor
is created and keep it open till step completion.
@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);
}
Is there a way to close this IndexWriter
after step completion?
Also, I was encountering issues because I do search also in this step to find duplicate documents but I fixed that by adding writer.commit();
before opening reader and searching.
Please suggest if I need to close and open after each document addition or can keep it open all along? and also how to close in StepExecutionListenerSupport
's afterStep
?
Initially, I was closing and reopening for each document but indexing process was very slow so I thought it might be the reason.