Java Lucene NRT 搜索不起作用

发布于 2025-01-02 12:14:31 字数 729 浏览 5 评论 0原文

玩转 Lucene。尝试让我的更改对其他阅读线程可见。无需重建索引。为此,我使用 SearcherManager 类。

创建管理器

Directory index = new SimpleFSDirectory(new File(LUCENE_INDEX_PATH));
w = new IndexWriter(index, config);
indexReader = IndexReader.open(w, true);
manager = new SearcherManager(w, true, null, null);

更新请求

w.updateDocument(t, document);
manager.maybeReopen(); // openIfChanged same behavior
w.commit();

搜索请求

IndexSearcher searcher = manager.acquire();
try {
    return performSearch(query, searcher, skip, limit);
} finally {
    manager.release(searcher);
    searcher = null;
}

更改将刷新到磁盘,但新的搜索请求仅在应用程序重新启动(重新创建索引)后才能看到它们。看起来 IndexSearcher 仍然指向旧索引。

Playing around with Lucene. Trying to make my changes visible for other reading threads. Without rebuilding index. For that purpose i use SearcherManager class.

Creation of manager

Directory index = new SimpleFSDirectory(new File(LUCENE_INDEX_PATH));
w = new IndexWriter(index, config);
indexReader = IndexReader.open(w, true);
manager = new SearcherManager(w, true, null, null);

Update request

w.updateDocument(t, document);
manager.maybeReopen(); // openIfChanged same behavior
w.commit();

Search request

IndexSearcher searcher = manager.acquire();
try {
    return performSearch(query, searcher, skip, limit);
} finally {
    manager.release(searcher);
    searcher = null;
}

Changes are flushed to disc, but new search request see them only after application restart (recreation of index). Looks like IndexSearcher still point to old Index.

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

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

发布评论

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

评论(3

清引 2025-01-09 12:14:31

您不需要打开自己的 IndexReader;只需创建 SearcherManager(从您的 IndexWriter),然后使用它的获取/释放来获取搜索器/读取器。

使用 writer 添加/删除文档后,您应该调用 MaybeReopen,然后下次调用 SearcherManager.acquire 时,返回的搜索器将反映更改。最好使用后台线程(即不是执行搜索的线程)来索引文档并调用 MaybeReopen。

您不需要调用 IndexWriter.commit 来使更改可见 - 仅当您需要持久性时才调用此方法(即,所有更改都安全地存储在磁盘上,并且能够在 OS/JVM 崩溃、断电、kill -9 等情况下幸存下来。 )。

You don't need to open your own IndexReader; just create the SearcherManager (from your IndexWriter) then use acquire/release from it, to get a searcher/reader.

After adding/deleting docs with the writer, you should call maybeReopen, and then the next time you call SearcherManager.acquire the returned searcher will reflect the changes. It's best to use a background thread (ie, not a thread doing searching) to index docs and call maybeReopen.

You don't need to call IndexWriter.commit to make changes visible -- only call this when you require durability (ie, that all changes are safely on disk and will survive OS/JVM crash, power loss, kill -9, etc.).

绝不放开 2025-01-09 12:14:31

如果您想使用 NRT 搜索,请使用不同的 SearcherManager 构造函数,该构造函数以 IndexWriter 作为参数。查看这篇文章http://blog.mikemccandless。 com/2011/11/near-real-time-readers-with-lucenes.html

If you want to use NRT search use different SearcherManager constructor that takes an IndexWriter as parameter. Check this article http://blog.mikemccandless.com/2011/11/near-real-time-readers-with-lucenes.html

月隐月明月朦胧 2025-01-09 12:14:31

看起来,我不应该创建 IndexReader。

indexReader = IndexReader.open(w, true)

如果我删除这一行,一切正常。还不知道为什么

Looks like, i should not create IndexReader.

indexReader = IndexReader.open(w, true)

If I remove this line, everything works. Dunno why yet

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