IndexNotFoundException if IndexSearcher caused an 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@af d07bb: files: [] 

Why is this happening?

+6
source share
2 answers

IndexSearcher expects a special directory structure that it cannot find because segments were not written (when you add documents to IndexWriter, they are queued in memory and when the amount of memory used reaches a specified threshold value or when commit (), these data structures in the memory is flushed to disk, which causes Lucene to call the segment).

What you need to do is explicitly create the segment by calling commit before opening IndexSearcher.

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

Also, this IndexWriter constructor is deprecated in Lucene 3.4, you'd better use IndexWriterConfig to configure IndexWriter:

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

Instead of calling an explicit commit, you can close IndexWriter, which must implicitly commit and close resources with lucene 4

0
source

Source: https://habr.com/ru/post/902337/


All Articles