* @throws SearchException in case of lock acquisition timeouts, IOException, or if a corrupt index is found
*/
public static void initializeIndexIfNeeded(Directory directory) {
//version doesn't really matter as we won't use the Analyzer
Version version = Environment.DEFAULT_LUCENE_MATCH_VERSION;
SimpleAnalyzer analyzer = new SimpleAnalyzer( version );
try {
if ( ! DirectoryReader.indexExists( directory ) ) {
try {
IndexWriterConfig iwriterConfig = new IndexWriterConfig( version, analyzer ).setOpenMode( OpenMode.CREATE );
//Needs to have a timeout higher than zero to prevent race conditions over (network) RPCs
//for distributed indexes (Infinispan but probably also NFS and similar)
iwriterConfig.setWriteLockTimeout( 2000 );
IndexWriter iw = new IndexWriter( directory, iwriterConfig );
iw.close();
}
catch (LockObtainFailedException lofe) {
log.lockingFailureDuringInitialization( directory.toString() );
}
}
}
catch (IOException e) {
throw new SearchException( "Could not initialize index", e );
}
finally {
analyzer.close();
}
}