4

假设我对添加和搜索文档有基本的了解。

管理 IndexWriter 和 IndexReader 实例的最佳实践是什么?

目前,我的应用程序创建了一个 IndexWriter 的单例实例。当我需要进行搜索时,我只需使用以下方法从 IndexWriter 创建一个 IndexSearcher

var searcher = new IndexSearcher(writer.GetReader())

我这样做是因为创建一个新的 IndexReader 会导致索引被加载到内存中,然后等待 GC 重新分配内存。这导致内存不足错误。

这个当前的实现是否被认为是理想的?这个实现解决了内存问题,但是 write.lock 文件总是存在的问题(因为 IndexWriter 总是被实例化并打开)。这是我在应用程序中遇到的错误的堆栈跟踪。

锁定获取超时:NativeFSLock@C:\inetpub\wwwroot\htdocs_beta\App_Data\products3\write.lock: System.IO.IOException: The process cannot access the file 'C:\inetpub\wwwroot\htdocs_beta\App_Data\products3\ write.lock' 因为它正被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs , String msgPath, Boolean bFromProxy, Boolean useLongPath) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at Lucene.Net.Store.NativeFSLock.Obtain()

我在想也许最好创建一个 IndexSearcher 的单例实例进行搜索,然后根据需要在内存中创建一个 IndexWriter。这样,在更新索引时将创建/删除 write.lock 文件。我看到的唯一问题是 IndexSearcher 实例将变得过时,如果索引已更新,我需要运行一个重新加载 IndexSearcher 的任务。

你怎么看?

您如何通过实时更新处理大型索引?

4

1 回答 1

1

您应该只使用一个索引编写器,以避免锁定问题。看看:Lucene.Net 写/读同步

于 2011-04-20T14:29:13.427 回答