正确的 LINQ to Lucene IndexASP.NET 的使用模式?
LINQ to Lucene 的 Index
的正确使用模式是什么?
它实现了 IDisposible,因此我认为将其包装在 using 语句中是最有意义的:
IEnumerable<MyDocument> documents = null;
using (Index<MyDocument> index = new Index<MyDocument>(new System.IO.DirectoryInfo(IndexRootPath)))
{
documents = index.Where(d => d.Name.Like("term")).ToList();
}
我偶尔会遇到磁盘上索引不必要的删除。如果同时存在索引的多个实例,则似乎 100% 的情况都会发生。我使用 PLINQ 编写了一个测试,并行运行 2 个搜索,其中 1 个搜索有效,而另一个则返回 0 个结果,因为索引已清空。
- 我应该使用单个静态实例吗?
- 我应该将其包装在
Lazy
中吗? - 当多个用户同时访问静态索引时,我是否会面临其他问题?
我还想根据需要定期重新索引,可能使用其他进程(例如 Windows 服务)。如果用户在重建索引时进行搜索,我是否也会遇到问题?
What is the proper usage pattern for LINQ to Lucene's Index<T>
?
It implements IDisposible
so I figured wrapping it in a using statement would make the most sense:
IEnumerable<MyDocument> documents = null;
using (Index<MyDocument> index = new Index<MyDocument>(new System.IO.DirectoryInfo(IndexRootPath)))
{
documents = index.Where(d => d.Name.Like("term")).ToList();
}
I am occasionally experiencing unwanted deleting of the index on disk. It seems happen 100% of the time if multiple instances of the Index exist at the same time. I wrote a test using PLINQ to run 2 searches in parallel and 1 search works while the other returns 0 results because the index is emptied.
- Am I supposed to use a single static instance instead?
- Should I wrap it in a
Lazy<T>
? - Am I then opening myself up to other issues when multiple users access the static index at the same time?
I also want to re-index periodically as needed, likely using another process like a Windows service. Am I also going to run into issues if users are searching while the index is being rebuilt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码类似于 Linq-to-Lucene。
大多数完全清除 Lucene 索引的情况是使用
create
参数设置为 true 创建的新 IndexWriter。问题中的代码不处理索引,因此进一步调试很困难。Lucene.Net 是线程安全的,我希望 linq-to-lucene 也能抑制这种行为。单个静态索引实例会将内容缓存在内存中,但我想您需要自己处理更改的索引重新加载(我不知道 linq-to-lucene 是否为您执行此操作)。
重新索引时使用多个搜索器/读取器应该没有问题,Lucene 就是为了支持这种情况而构建的。但是,每个目录只能有一个写入器,因此当您的 Windows 服务优化索引时,没有其他进程可以将文档写入索引。
The code looks like Linq-to-Lucene.
Most cases of completely cleared Lucene indexes are new IndexWriters created with the
create
parameter set to true. The code in the question does not handle indexing so debugging this further is difficult.Lucene.Net is thread-safe, and I expect linq-to-lucene to also inhibit this behavior. A single static index instance would cache stuff in memory, but I guess you'll need to handle index reloading of changes yourself (I do not know if linq-to-lucene does this for you).
There should be no problems using several searchers/readers when reindexing, Lucene is build to support that scenario. However, there can only be one writer per directory, so no other process can write documents to the index while your windows service were to optimize the index.