并发写入Lucene索引

发布于 2025-01-02 11:25:54 字数 690 浏览 2 评论 0原文

一个索引由多个 IndexWriter 共享。显然,如果正在打开的索引将被其他 IndexWriter 打开,则会抛出 LockObtainFailedException 异常。 我的解决方案是创建具有较长超时的 IndexWriter:

IndexWriterConfig conf= new IndexWriterConfig(Version.LUCENE_30, new SimpleAnalyzer (Version.LUCENE_30));
conf.setWriteLockTimeout(30*1000);//Wait 30 seconds timeout
try{
        IndexWriter writer = new IndexWriter(dir, conf);
}catch(LockObtainFailedException e){
        System.out.println("give up trying...");
}

对于这种情况有更好的解决方案吗?

编辑:受到蒂洛的启发。我发现 IndexWriter 的 JavaDoc:

IndexWriter 实例是完全线程安全的,这意味着多个线程可以同时调用它的任何方法。如果您的应用程序需要外部同步,则不应在 IndexWriter 实例上进行同步,因为这可能会导致死锁;请使用您自己的(非 Lucene)对象。

An index is shared by multiple IndexWriters. Obviously, LockObtainFailedException will be thrown if an opening index is about to open by other IndexWriter.
My solution is to create IndexWriter with long timeout:

IndexWriterConfig conf= new IndexWriterConfig(Version.LUCENE_30, new SimpleAnalyzer (Version.LUCENE_30));
conf.setWriteLockTimeout(30*1000);//Wait 30 seconds timeout
try{
        IndexWriter writer = new IndexWriter(dir, conf);
}catch(LockObtainFailedException e){
        System.out.println("give up trying...");
}

Any better solution for this case?

EDIT: inspired by Thilo. I found JavaDoc of IndexWriter:

IndexWriter instances are completely thread safe, meaning multiple threads can call any of its methods, concurrently. If your application requires external synchronization, you should not synchronize on the IndexWriter instance as this may cause deadlock; use your own (non-Lucene) objects instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2025-01-09 11:25:54

单个Directory不能被多个IndexWriters共享。只需在您的线程中共享一个 IndexWriter 即可。

A single Directory cannot be shared by multiple IndexWriters. Just share one IndexWriter amongst your threads.

避讳 2025-01-09 11:25:54

IndexWriter 是完全线程安全的,因此对其进行多线程操作是安全的,但同一个 Index 上不能有多个 IndexWriter。

IndexWriter is totally ThreadSafe , so mutlthread operation on it is safe , but u can't have more than one IndexWriter over the same Index .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文