2013-03-18 312 views
4

我想索引從tomcat服務器獲取的一大組日誌文件。我編寫了代碼打開每個文件,爲每行創建索引,然後使用Apache lucene存儲每行。所有這些都是使用多線程完成的。org.apache.lucene.store.LockObtainFailedException:鎖定獲取超時:

我得到這個異常,當我嘗試這種代碼

org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out: 

代碼

if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) 
     { 
      // New index, so we just add the document (no old document can be there): 
      System.out.println("adding " + path); 

       indexWriter.addDocument(doc); 

     } else { 
      // Existing index (an old copy of this document may have been indexed) so 
     // we use updateDocument instead to replace the old one matching the exact 
      // path, if present: 
      System.out.println("updating " + path); 

       indexWriter.updateDocument(new Term("path", path), doc); 

      } 
     indexWriter.commit(); 
     indexWriter.close(); 

現在我想,因爲我每次都犯了索引,則可能會導致一個寫鎖。所以我刪除indexWriter.commit();

if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) 
    { 
     // New index, so we just add the document (no old document can be there): 
     System.out.println("adding " + path); 

      indexWriter.addDocument(doc); 

    } else { 
     // Existing index (an old copy of this document may have been indexed) so 
    // we use updateDocument instead to replace the old one matching the exact 
     // path, if present: 
     System.out.println("updating " + path); 

      indexWriter.updateDocument(new Term("path", path), doc); 

     } 

    indexWriter.close(); 

現在我得到也不例外

問:所以我的問題是,爲什麼indexWriter.commit();導致異常。即使我刪除indexWriter.commit();我在搜索時沒有遇到任何問題。那是我得到我想要的確切結果。那麼爲什麼要使用indexWriter.commit(); ?

回答

2

簡而言之,它與DB提交類似,除非您提交事務,將添加到Solr的文檔只保存在Memory中。只有在提交時,文檔纔會被保留在索引中。
如果Solr在文檔在內存中時崩潰,則可能會丟失這些文檔。

Explanation: -

一個從一開始在Lucene的原則是一次寫入 政策。我們從不寫兩次文件。當您通過 IndexWriter添加文檔時,它會被索引到內存中,一旦達到某個閾值(最大緩衝文檔或RAM緩衝區大小),我們會將所有文檔從主內存寫入磁盤;你可以在這裏和這裏找到更多關於此的 。將文檔寫入磁盤會產生一個稱爲段的新索引。現在,當您索引一堆文檔 或者在生產中運行增量索引時,您可以看到 數量的段經常變化。但是,一旦你調用提交 Lucene將其整個RAM緩衝區刷新爲段,同步它們並且 將屬於該提交的所有段的指針寫入 段文件

如果文檔已經存在於Solr中,它將被覆蓋(由唯一的ID確定)。
因此,您的搜索仍然可以正常工作,但最新文檔不可用於搜索,除非您提交。

此外,一旦您打開並編寫索引,它將在索引上獲得鎖定,並且應關閉寫入程序以釋放鎖定。