放牧代码和思想
专注自然语言处理、机器学习算法
    愛しさ 優しさ すべて投げ出してもいい

解决org.apache.lucene.store.AlreadyClosedException: this Directory is closed

在Lucene中,关闭一个IndexWriter时抛出AlreadyClosedException异常:

org.apache.lucene.store.AlreadyClosedException: this Directory is closed

at org.apache.lucene.store.BaseDirectory.ensureOpen(BaseDirectory.java:66) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.store.FSDirectory.deleteFile(FSDirectory.java:269) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexFileDeleter.deleteFile(IndexFileDeleter.java:595) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexFileDeleter.deleteNewFiles(IndexFileDeleter.java:582) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.deleteNewFiles(IndexWriter.java:4693) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.DocumentsWriter$DeleteNewFilesEvent.process(DocumentsWriter.java:739) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.processEvents(IndexWriter.java:4746) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.processEvents(IndexWriter.java:4738) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.doFlush(IndexWriter.java:3198) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.flush(IndexWriter.java:3170) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.closeInternal(IndexWriter.java:991) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:935) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:897) ~[lucene-core-4.8.1.jar:4.8.1 1594670 – rmuir – 2014-05-14 19:22:52]

有人说这是因为index目录下的write.lock没有被删除,其实并不是这样。

真正的原因是,IndexWriter依赖于一个Directory,当Directory关闭的时候,IndexWriter也就随之关闭了。所以正确的关闭顺序是

  1. 先关闭IndexWriter

  2. 后关闭Directory

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » 解决org.apache.lucene.store.AlreadyClosedException: this Directory is closed

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的作品

HanLP自然语言处理包《自然语言处理入门》