public LuceneIndexer(String indexName) throws IOException { this.indexName = indexName; luceneWriterService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new NamedThreadFactory(threadGroup, indexName + " Lucene writer")); luceneWriterFutureCheckerService = Executors.newFixedThreadPool(1, new NamedThreadFactory(threadGroup, indexName + " Lucene future checker")); setupRoot(); File indexDirectoryFile = new File(root.getPath() + "/" + indexName); System.out.println("Index: " + indexDirectoryFile); Directory indexDirectory = initDirectory(indexDirectoryFile); indexDirectory.clearLock("write.lock"); IndexWriterConfig config = new IndexWriterConfig(luceneVersion, new StandardAnalyzer(luceneVersion)); MergePolicy mergePolicy = new LogByteSizeMergePolicy(); config.setMergePolicy(mergePolicy); config.setSimilarity(new ShortTextSimilarity()); IndexWriter indexWriter = new IndexWriter(indexDirectory, config); trackingIndexWriter = new NRTManager.TrackingIndexWriter(indexWriter); boolean applyAllDeletes = false; searcherManager = new NRTManager(trackingIndexWriter, null, applyAllDeletes); // Refreshes searcher every 5 seconds when nobody is waiting, and up to 100 msec delay // when somebody is waiting: reopenThread = new NRTManagerReopenThread(searcherManager, 5.0, 0.1); this.startThread(); }
@PostConstruct public void afterPropertiesSet() throws IOException { if( !indexPath.exists() ) { if( !indexPath.mkdirs() ) { throw new Error("Error creating index:" + indexPath); //$NON-NLS-1$ } } directory = FSDirectory.open(indexPath); if( IndexWriter.isLocked(directory) ) { LOGGER.info("Unlocking index:" + indexPath); //$NON-NLS-1$ IndexWriter.unlock(directory); } LOGGER.info("Opening writer for index:" + indexPath); //$NON-NLS-1$ indexWriter = new IndexWriter(directory, new IndexWriterConfig(LuceneConstants.LATEST_VERSION, getAnalyser())); nrtManager = new NRTManager(indexWriter, null); // Possibly reopen a searcher every 5 seconds if necessary in the // background nrtReopenThread = new NRTManagerReopenThread(nrtManager, 5.0, 0.1); nrtReopenThread.setName("NRT Reopen Thread: " + getClass()); nrtReopenThread.setPriority(Math.min(Thread.currentThread().getPriority() + 2, Thread.MAX_PRIORITY)); nrtReopenThread.setDaemon(true); nrtReopenThread.start(); // Commit any changes to disk every 5 minutes commiterThread = new Timer(true); commiterThread.schedule(new TimerTask() { @Override public void run() { try { indexWriter.commit(); } catch( IOException ex ) { LOGGER.error("Error attempting to commit index writer", ex); } } }, 5 * 60 * 1000, 5 * 60 * 1000); }