Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 576
Reactormonk Avatar asked Nov 24 '11 22:11

Reactormonk


1 Answers

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)
like image 84
jpountz Avatar answered Sep 28 '22 08:09

jpountz