Whoops!
This is "straight Java" Lucene, but it may well apply to other varieties.
In Lucene 4.0.0 the API for DirectoryReader.indexExists()
says
Returns true if an index exists at the specified directory.
But in Lucene 4.10.2 the API for DirectoryReader.indexExists()
says
Returns true if an index likely exists at the specified directory.
Note that if a corrupt index exists, or if an index in the process of
committing
... yes, it breaks off mid-sentence. NB I compiled my Javadoc direct from the source, but the same unfinished phrase can be seen in the online API. Not only that, but I looked at the Lucene 6.0.0 API, and it is exactly the same.
The "returns" phrase is however:
true if an index exists; false otherwise
... but I currently believe an empty directory will sometimes (?) return true
(from my unit testing). Anyway, I wouldn't trust it.
If you create an IndexReader
on an empty directory, it appears that all its methods will return without throwing exceptions. You can go indexReader.numDocs()
, and this will return 0, but that doesn't prove that there is no index there, only that there are no Document
s. Depending on your requirements that might be enough, of course.
Similarly, you can create an IndexSearcher
from such an IndexReader
, and you can create an IndexWriter
. None of these will have any apparent problem with an empty directory.
BETTER SOLUTION:
try {
directoryReader = DirectoryReader.open( fsDir );
} catch ( org.apache.lucene.index.IndexNotFoundException e) {
...
}
This appears, as far as I can tell, to be reliable.
DirectoryReader.indexExists
now: lucene.apache.org/core/8_1_0/core/org/apache/lucene/index/… – Haunch