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