问题描述
当将 Lucene.Net 与 ASP.NET 一起使用时,我可以想象一个 Web 请求可以触发对索引的更新,而另一个 Web 请求正在执行搜索.Lucene.Net 是否内置了管理并发访问的能力,还是我必须对其进行管理以避免被另一个进程使用"错误?
阅读文档和实验后,我认为我学到了:有两个问题,线程安全和并发.多线程是安全的",因为您不能对索引做任何坏事.但是,这是安全的,代价是一次只有一个对象锁定索引.第二个对象会出现并抛出异常.因此,您不能打开搜索并期望另一个线程中的作者能够更新索引.如果一个线程忙于更新索引,那么尝试创建搜索器将会失败.
此外,搜索者看到的索引与他们打开时的状态相同,因此如果您保留它们并更新索引,他们将不会看到更新.
我希望我的搜索者看到最新的更新.
我的设计,而且目前看来是有效的,是我的作者和搜索者共享一个锁,这样他们就不会失败 - 他们只是等待 - 直到当前的写入或搜索完成.
根据本页,p><块引用>
索引和搜索不仅线程安全,但过程安全.什么这意味着:
- 多个索引搜索者可以阅读lucene 索引文件.
- 索引作者或读者可以编辑搜索时的 lucene 索引文件正在进行中
- 多个索引编写器或读者可以尝试编辑lucene同时索引文件(它是对索引作者/读者很重要被关闭,所以它会释放文件锁).但是,查询解析器不是线程安全的,所以每个线程使用索引应该有自己的查询解析器.
然而,索引编写器是线程安全,因此您可以更新索引当人们在搜索它时.但是,您必须确保具有开放索引的线程搜索者关闭它们并打开新的,以获取最新更新的数据.
When using Lucene.Net with ASP.NET, I can imagine that one web request can trigger an update to the index while another web request is performing a search. Does Lucene.Net have built in it the ability to manage concurrent access, or do I have to manage it, to avoid "being used by another process" errors?
EDIT: After reading docs and experimentation, this is what I think I've learned: There are two issues, thread safety and concurrency. Multithreading is "safe" in that you can't do anything bad to the index. But, it's safe at the cost of just one object having a lock on the index at one time. The second object will come along and throw an exception. So, you can't leave a search open and expect a writer in another thread to be able to update the index. And if a thread is busy updating the index, then trying to create a searcher will fail.
Also, Searchers see the index as it was at the time that they open it, so if you keep them around, and update the index, they won't see the updates.
I wanted my searchers to see the latest updates.
My design, and it seems to be working so far, is that my writers and searchers share a lock, so that they don't fail - they just wait - until the current write or search is done.
According to 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.
这篇关于Lucene.Net 是否管理访问同一索引的多个线程,一个索引而另一个正在搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!