微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果IndexSearcher调用空RAMDirectory,则为IndexNotFoundException

# some java_imports here
index = RAMDirectory.new
IndexWriter.new(index,StandardAnalyzer.new(Version::LUCENE_30),IndexWriter::MaxFieldLength::UNLIMITED )
IndexSearcher.new(index)

生成

NativeException: org.apache.lucene.index.IndexNotFoundException: no segments* file found in org.apache.lucene.store.RAMDirectory@668c640e lockFactory=org.apache.lucene.store.SingleInstanceLockFactory@afd07bb: files: []

为什么会这样?

解决方法

IndexSearcher需要一个特殊的目录结构,它找不到因为没有写入段(当你向IndexWriter添加文档时,它们在内存中排队,当使用的内存量达到给定的阈值或者commit()时这些内存中的数据结构被刷新到磁盘,导致Lucene称之为段.

您需要做的是通过在打开IndexSearcher之前调用commit来明确地创建一个段.

index = RAMDirectory.new
writer = IndexWriter.new(index,IndexWriter::MaxFieldLength::UNLIMITED)
writer.commit()
IndexSearcher.new(index)

此外,在Lucene 3.4中不推荐使用此IndexWriter构造函数,您应该使用IndexWriterConfig为IndexWriter配置:

iwConfig = IndexWriterConfig.new(Version::LUCENE_34,StandardAnalyzer.new(Version::LUCENE_34))
writer = IndexWriter.new(index,iwConfig)

原文地址:https://www.jb51.cc/ruby/265727.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐