Reactormonk
Reactormonk

Reputation: 21740

IndexNotFoundException if IndexSearcher called on empty RAMDirectory

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

Upvotes: 6

Views: 4064

Answers (2)

nir
nir

Reputation: 3868

Instead of calling explicit commit, you can make sure to close IndexWriter which should implicitly commits and close resources as of lucene 4

Upvotes: 0

jpountz
jpountz

Reputation: 9964

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)

Upvotes: 11

Related Questions