Before calling AddDocument()
on IndexWriter
, is it okay if I call
在IndexWriter上调用AddDocument()之前,如果我调用它是否可以
IndexReader.IsLocked(myDirectory)
and if it returns true, then call
如果它返回true,则调用
IndexReader.Unlock(myDirectory)
i.e.
if(IndexReader.IsLocked(myDirectory))
{
IndexReader.Unlock(myDirectory);
}
writer = new IndexWriter(myDirectory, _analyzer, true);
writer.AddDocument(doc);
I keep getting "Lock obtain timed out." errors in my code. To overcome this error, I plan to this approach if it is okay.
我一直在“锁定获得超时”。我的代码中的错误。为了克服这个错误,我计划这种方法,如果它没问题。
2 个解决方案
#1
Getting the "Lock obtain timed out" error is a warning sign that something is wrong with the way you handle your index. If you have more than one IndexWriter
writing to the index, forcing unlock would likely cause your index to get corrupted.
获取“锁定获取超时”错误是一个警告信号,表明您处理索引的方式有问题。如果您有多个IndexWriter写入索引,强制解锁可能会导致您的索引损坏。
However, in my experience it's easy to get those errors when you're working on the code, since the occasional crashes and interrupted debug sessions can leave you index locked, even though no process is writing to it anymore.
但是,根据我的经验,当您处理代码时很容易发现这些错误,因为偶尔崩溃和中断的调试会话可能会使索引锁定,即使没有进程正在写入它。
If that is the case, it would be OK to unlock the index at the start of the process. Don't call it every time before calling addDocument
, just once when creating the IndexWriter
.
如果是这种情况,可以在流程开始时解锁索引。在调用addDocument之前不要每次调用它,只需在创建IndexWriter时调用一次。
In any case, make sure you close all IndexWriters properly before exiting the process.
在任何情况下,请确保在退出进程之前正确关闭所有IndexWriters。
#2
Important point to remember with Lucene, only one thread should be updating the index... so there are concurrent users on the website, but one only user should be updating.
要记住Lucene的重要一点,只有一个线程应该更新索引...所以网站上有并发用户,但只有一个用户应该更新。
If you do not handle that properly, you will run into problems... You can have multiple reads/queries but not writes
如果你没有正确处理,你会遇到问题......你可以有多个读/查询但不能写
#1
Getting the "Lock obtain timed out" error is a warning sign that something is wrong with the way you handle your index. If you have more than one IndexWriter
writing to the index, forcing unlock would likely cause your index to get corrupted.
获取“锁定获取超时”错误是一个警告信号,表明您处理索引的方式有问题。如果您有多个IndexWriter写入索引,强制解锁可能会导致您的索引损坏。
However, in my experience it's easy to get those errors when you're working on the code, since the occasional crashes and interrupted debug sessions can leave you index locked, even though no process is writing to it anymore.
但是,根据我的经验,当您处理代码时很容易发现这些错误,因为偶尔崩溃和中断的调试会话可能会使索引锁定,即使没有进程正在写入它。
If that is the case, it would be OK to unlock the index at the start of the process. Don't call it every time before calling addDocument
, just once when creating the IndexWriter
.
如果是这种情况,可以在流程开始时解锁索引。在调用addDocument之前不要每次调用它,只需在创建IndexWriter时调用一次。
In any case, make sure you close all IndexWriters properly before exiting the process.
在任何情况下,请确保在退出进程之前正确关闭所有IndexWriters。
#2
Important point to remember with Lucene, only one thread should be updating the index... so there are concurrent users on the website, but one only user should be updating.
要记住Lucene的重要一点,只有一个线程应该更新索引...所以网站上有并发用户,但只有一个用户应该更新。
If you do not handle that properly, you will run into problems... You can have multiple reads/queries but not writes
如果你没有正确处理,你会遇到问题......你可以有多个读/查询但不能写