如何检查 Lucene IndexWriter 实例是否有效/打开?

发布于 2024-10-12 19:24:27 字数 758 浏览 7 评论 0 原文

抱歉这个简单的问题,但似乎没有任何明显的方法。根据文档,建议在内存中保留一个 IndexWriter 实例,以便一次又一次地用于更新,而不是为每次更改打开和关闭一个实例(这样成本要高得多) )。

然而,文档还指出,如果发生异常(例如,OutOfMemoryException),则应关闭IndexWriter

因此,我需要某种方法来检查我的 IndexWriter 实例是否有效,假设它可以通过其他操作关闭。

我意识到如果我尝试在关闭时使用它,我会得到一个异常,但我不想失败,而是想在需要它之前检查一下是否需要创建一个新的 IndexWriter 所以不需要发生故障。

在我看来,一个简单的 IsOpen 属性/方法就是如此明显的补充。

想法:

我想,如果我能确保我的类是唯一能够关闭 IndexWriter 的类,我可以简单地将其设置为 null当它关闭时,只要检查以确保它不是 null 每当我要使用它时,但这不会处理 IndexWriter 自行关闭的情况(如果在这种情况下)现在或将来是可能的)。

另一个想法是,我可以将使用它的尝试包装在 try/catch 块中,并使用异常来指示它需要重新打开,但这似乎不是很有效/优雅。我应该使用什么方法来测试 IndexWriter 的有效性?

Sorry for the simple question, but there doesn't seem to be any obvious way. According to the documentation, it is recommended to keep a single instance of IndexWriter in memory that can be used again and again for updates, as opposed to opening and closing one for each change (which is much more costly).

However, the documentation also states that the IndexWriter should be closed if an exception occurs (e.g. OutOfMemoryException).

Therefore, I need some way to check if my instance of IndexWriter is valid or not, assuming it could be closed by some other operation.

I realize I will get an exception if I try to use it when it's closed, but rather than fail, I would like to check to see if I need to create a new IndexWriter before I need it so no failure occurs.

It seems to me a simple IsOpen property/method would be such an obvious addition.

Thoughts:

I suppose that if I could ensure that my class is the only thing capable of closing the IndexWriter, I could simply set it to null when it was closed and just check to make sure it's not null whenever I go to use it, however this would not handle cases where the IndexWriter closes itself (if such a case is possible now or in the future).

Another thought is that I could wrap an attempt to use it in a try/catch block and use the exception to indicate that it needs to be reopened, but this doesn't seem very efficient/elegant. What method would I use to test the validity of the IndexWriter?

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

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

发布评论

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

评论(2

失与倦" 2024-10-19 19:24:27

如果您遇到内存不足异常(或任何类型的异常,实际上),这可能是一个大问题。您尝试写入的更改可能不会被写入,您的索引甚至可能已损坏,具体取决于异常发生的时间/方式。

因此,处于错误状态的 IndexWriter 是一种特殊情况,我想说它肯定需要使用异常,即 try/catch。

(我不认为你的问题是“重新打开编写器”。根据异常情况,你可能需要重新索引内容,可能从头开始。当然,你不应该期望在编写器中经常出现异常。)

If you get an out of memory exception (or any kind of exception, really), that's potentially a big problem. The changes you were trying to write probably won't get written, and your index may even be corrupt, depending on when/how the exception occurred.

So an IndexWriter being in a faulted state is an exceptional circumstance, and I would say it certainly warrants using exceptions, i.e. try/catch.

(I don't think your catch would be "reopen the writer." Depending on the exception, you might need to reindex stuff, possibly from scratch. Certainly you shouldn't expect to get exceptions in your writer as a regular occurrence.)

爱的十字路口 2024-10-19 19:24:27

我想 IndexWriter 会抛出 AlreadyClosedException (参见 ensureOpen),如果它在访问时关闭。因此,如果 IndexWriter 不为 null,您可以处理该问题。

华泰

I suppose the IndexWriter would throw an AlreadyClosedException (see ensureOpen), if it is closed on access. So you could handle that in case the IndexWriter is not null.

HTH

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