4

我是 Lucene 搜索 API 的新手。更新 Lucene 索引时,我不断收到以下异常...为什么会出现此错误以及如何避免它?

System.IO.IOException: Lock obtain timed out: SimpleFSLock@C:\Indexes\write.lock
   at Lucene.Net.Store.Lock.Obtain(Int64 lockWaitTimeout)
   at Lucene.Net.Index.IndexWriter.Init(Directory d, Analyzer a, Boolean create, Boolean closeDir)
   at Lucene.Net.Index.IndexWriter.Init(String path, Analyzer a, Boolean create)
   at Lucene.Net.Index.IndexWriter..ctor(String path, Analyzer a, Boolean create)

谢谢阅读。

4

3 回答 3

11

Lucene 在以写模式打开索引时创建锁定文件。当您完全关闭索引时,此锁定文件将被删除。在写入索引时,如果程序在没有关闭 lucene IndexWriter 的情况下退出,下次尝试写入时会出现此异常。如果您从索引目录中删除锁定文件,您将不会看到此异常。

您可以选择使用FSDirectory.setDisableLocks(false)禁用锁定,但这是不可取的,因为错误会被静默忽略。

于 2009-05-08T06:51:00.020 回答
1

我认为,如果索引写入花费的时间超过超时值,并且在锁定到期后和索引写入完成之前出现另一个索引写入,您也可能会收到此错误 - 至少看起来这是正在发生的事情。

当我开始接近单个 Windows 目录中最大文件数的 50% 左右时,我开始遇到同样的问题。这减慢了写入速度,足以导致问题。(Win2k3)

我相信你可以重新编译Lucene来改变超时值。

于 2009-05-08T14:38:32.680 回答
0

尝试以下操作:

try
{
    writer = new IndexWriter(directory, new StandardAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
}
catch (LockObtainFailedException ex)
{
    DirectoryInfo indexDirInfo = new DirectoryInfo(directory);
    FSDirectory indexFSDir = FSDirectory.Open(indexDirInfo, new Lucene.Net.Store.SimpleFSLockFactory(indexDirInfo));
    IndexWriter.Unlock(indexFSDir);
    writer = new IndexWriter(directory, new StandardAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
}
于 2012-09-24T06:00:43.727 回答