这是我的问题,我执行 add() 将文档添加到我的索引,然后我关闭它。效果很好!
现在我有一个新需求,每次我在我的数据库中保存一些东西时,我都需要更新我的索引。我无法再次创建 indexWriter,因为它需要超过 4 分钟,所以我只需要 update() 或 add() 一个文档到索引。
为了完成它,我没有执行 index.close(),而是在填充索引后执行 index.commit()...但我认为它应该关闭然后打开以更新 ()。
有什么建议吗?谢谢!
最佳答案
close
是一项代价高昂的操作,javadocs 建议 commit
如果您经常更新,请使用。 javadoc 声明 close
:
Commits all changes to an index and closes all associated files. Note that this may be a costly operation, so, try to re-use a single writer instead of closing and opening a new one.
我认为 close
和 commit
之间的区别在于 commit
仅刷新数据以使其对读者可见,而 close
也优化了索引。这使得 commit
关于 5 times faster比关闭
。
如果您连续添加数据,最好先提交
,然后在完成后最后关闭
。
关于java - Lucene - 打开一个关闭的 IndexWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3865183/