如何检查Lucene IndexWriter实例是否有效/打开? [英] How can I check if a Lucene IndexWriter instance is valid/open?

查看:162
本文介绍了如何检查Lucene IndexWriter实例是否有效/打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉这个简单的问题,但是似乎没有任何明显的方法.根据文档,建议将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).

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

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

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

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.

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

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.

在我看来,简单的IsOpen属性/方法将是显而易见的补充.

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

想法:

我想如果我能确保我的班级是唯一能够关闭IndexWriter的东西,那么我可以在关闭时简单地将其设置为null,并且只要确保它不是null就可以进行检查.我将使用它,但是这将无法处理IndexWriter关闭自身的情况(如果现在或将来可能发生这种情况).

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).

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

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?

推荐答案

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

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.

因此,处于故障状态的IndexWriter是一种特殊情况,我想说肯定可以保证使用异常(即try/catch).

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.)

这篇关于如何检查Lucene IndexWriter实例是否有效/打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆