2013-03-18 309 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确定)。
因此,您的搜索仍然可以正常工作,但最新文档不可用于搜索,除非您提交。

此外,一旦您打开并编写索引,它将在索引上获得锁定,并且应关闭写入程序以释放锁定。