2016-09-26 62 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(); 
相關問題