# some java_imports here
index = RAMDirectory.new
IndexWriter.new(index, StandardAnalyzer.new(Version::LUCENE_30), IndexWriter::MaxFieldLength::UNLIMITED )
IndexSearcher.new(index)
generates
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: []
Why does this happen?
The IndexSearcher expects a special directory structure, which it cannot find because no segments have been written (when you add documents to an IndexWriter, they are queued in memory, and when the amount of used memory reaches a given threshold or when commit() is called, these in-memory data structures are flushed to disk resulting in what Lucene calls a segment).
What you need to do is to explicitely create a segment by calling commit before opening your IndexSearcher.
index = RAMDirectory.new
writer = IndexWriter.new(index, StandardAnalyzer.new(Version::LUCENE_30),IndexWriter::MaxFieldLength::UNLIMITED)
writer.commit()
IndexSearcher.new(index)
Moreover this IndexWriter constructor is deprecated in Lucene 3.4, you should rather use IndexWriterConfig to configure you IndexWriter:
iwConfig = IndexWriterConfig.new(Version::LUCENE_34, StandardAnalyzer.new(Version::LUCENE_34))
writer = IndexWriter.new(index, iwConfig)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With