当使用Lucene.Net与ASP.NET时,我可以想象一个Web请求可以触发对索引的更新,而另一个Web请求执行搜索. Lucene.Net是否已经建立了管理并发访问的能力,或者我必须管理它,以避免“被另一个进程使用”的错误?
编辑:阅读文档和实验后,这是我认为我学到的:线程安全和并发有两个问题.多线程是“安全的”,因为你不能对索引做任何坏事.但是,只要一个对象同时在索引上锁定就是安全的.第二个对象将会出现并抛出异常.所以,你不能让搜索打开,并期望另一个线程中的作者能够更新索引.如果线程忙于更新索引,则尝试创建搜索器将失败.
此外,搜索者会看到索引,因为它是在它们打开的时候,所以如果你保持它们,并更新索引,他们将看不到更新.
我希望我的搜索者能够看到最新的更新.
解决方法
根据
this page,
Indexing and searching are not only
thread safe,but process safe. What
this means is that:
- Multiple index searchers can read the
lucene index files at the same time.- An index writer or reader can edit the
lucene index files while searches are
ongoing- Multiple index writers or
readers can try to edit the lucene
index files at the same time (it’s
important for the index writer/reader
to be closed so it will release the
file lock). However,the query parser
is not thread safe,so each thread
using the index should have its own
query parser.The index writer however,is thread safe,so you can update the index while people are searching it. However,you then have to make sure that the threads with open index searchers close them and open new ones,to get the newly updated data.