gpt4 book ai didi

asp.net - Lucene.Net 是否管理访问同一索引的多个线程,一个索引而另一个正在搜索?

转载 作者:行者123 更新时间:2023-12-03 20:08:29 25 4
gpt4 key购买 nike

在 ASP.NET 中使用 Lucene.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.

关于asp.net - Lucene.Net 是否管理访问同一索引的多个线程,一个索引而另一个正在搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/193624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com