/** * Initialize the Lucene Directory if it isn't already. * * @param directory the Directory to initialize * @throws SearchException in case of lock acquisition timeouts, IOException, or if a corrupt index is found */ public static void initializeIndexIfNeeded(Directory directory) { SimpleAnalyzer analyzer = new SimpleAnalyzer(); try { if ( ! DirectoryReader.indexExists( directory ) ) { try { IndexWriterConfig iwriterConfig = new IndexWriterConfig( analyzer ).setOpenMode( OpenMode.CREATE_OR_APPEND ); //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) SleepingLockWrapper delayedDirectory = new SleepingLockWrapper( directory, 2000, 20 ); IndexWriter iw = new IndexWriter( delayedDirectory, iwriterConfig ); iw.close(); } catch (LockObtainFailedException lofe) { log.lockingFailureDuringInitialization( directory.toString() ); } } } catch (IOException e) { throw new SearchException( "Could not initialize index", e ); } finally { analyzer.close(); } }
/** * Initialize the Lucene Directory if it isn't already. * * @param directory the Directory to initialize * @throws SearchException in case of lock acquisition timeouts, IOException, or if a corrupt index is found */ public static void initializeIndexIfNeeded(Directory directory) { SimpleAnalyzer analyzer = new SimpleAnalyzer(); try { if ( ! DirectoryReader.indexExists( directory ) ) { try { IndexWriterConfig iwriterConfig = new IndexWriterConfig( analyzer ).setOpenMode( OpenMode.CREATE_OR_APPEND ); //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) SleepingLockWrapper delayedDirectory = new SleepingLockWrapper( directory, 2000, 20 ); IndexWriter iw = new IndexWriter( delayedDirectory, iwriterConfig ); iw.close(); } catch (LockObtainFailedException lofe) { log.lockingFailureDuringInitialization( directory.toString() ); } } } catch (IOException e) { throw new SearchException( "Could not initialize index", e ); } finally { analyzer.close(); } }