2016-09-26 63 views
0

正在关闭Lucene IndexWriter在每个文件添加后减慢我的索引过程?我应该让Lucene IndexWriter在整个索引中保持打开状态,还是在每次添加文档后关闭?

我想象一下,关闭和打开索引编写器会减慢我的索引过程,还是对于Lucene来说不正确?

基本上,我在Spring批处理作业中有一个Lucene索引器步骤,我在ItemProcessor中创建索引。索引器步骤是一个分区步骤,我创建了IndexWriter,当创建ItemProcessor并保持打开状态直至完成步骤。

@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();来解决此问题。

请建议在每次添加文档之后是否需要关闭并打开,或者始终保持打开状态?以及如何关闭StepExecutionListenerSupportafterStep

最初,我正在关闭并重新打开每个文档,但索引过程非常缓慢,所以我认为这可能是原因。

+0

您应该*确保*为整个索引过程保持一个“IndexWriter”打开。正如您已经看到的,为每个文档打开一个新文档预计会大大减缓它的速度。 – femtoRgon

回答

0

由于在开发过程中,索引目录的大小很小,所以我们可能看不到太多的收益,但是对于大索引目录大小,我们不需要为IndexWriter以及IndexReader进行不必要的创建和关闭。

在Spring Batch的,我与这些步骤

1.As在my other question指出实现它,首先我们需要解决系列化的问题把对象ExecutionContext

2.我们创建复合可序列化对象的实例并将其放入分区器中的ExecutionContext

ExecutionContext到你的脚步阅读器,处理器或作家在配置

3.Pass值,

@Bean 
    @StepScope 
    public ItemProcessor<InputVO,OutputVO> luceneIndexProcessor(@Value("#{stepExecutionContext[field1]}") String field1,@Value("#{stepExecutionContext[luceneObjects]}") SerializableLuceneObjects luceneObjects) throws Exception{ 
     LuceneIndexProcessor indexProcessor =new LuceneIndexProcessor(luceneObjects); 
     return indexProcessor; 
    } 

4.使用这种情况下处理器通过你需要的地方,并使用getter方法来获得指数读者或作家, public IndexWriter getLuceneIndexWriter() {return luceneIndexWriter;}

5.最后在StepExecutionListenerSupportafterStep(StepExecution stepExecution)ExecutionContext关闭此作者或读者。

ExecutionContext executionContext = stepExecution.getExecutionContext(); 
SerializableLuceneObjects slObjects = (SerializableLuceneObjects)executionContext.get("luceneObjects"); 
IndexWriter luceneIndexWriter = slObjects.getLuceneIndexWriter(); 
IndexReader luceneIndexReader = slObjects.getLuceneIndexReader(); 
if(luceneIndexWriter !=null) luceneIndexWriter.close(); 
if(luceneIndexReader != null) luceneIndexReader.close(); 
相关问题