@Override public void close() { CloseUtil.close(indexSearcher); CloseUtil.close(indexReader); CloseUtil.close(indexWriter); CloseUtil.close(directory); opened.set(false); if (logger.isWarnEnabled()) { logger.warn("search engine has been closed."); } }
public OperationResponse commitAndOptimize() { try { if (logger.isDebugEnabled()) { logger.debug("commiting..."); } indexWriter.commit(); if (logger.isDebugEnabled()) { logger.debug("commit finish."); } if (logger.isDebugEnabled()) { logger.debug("optimizing..."); } indexWriter.forceMerge(defaultMergeSize); if (logger.isDebugEnabled()) { logger.debug("optimize finish."); } reopenSearcher(); } catch (IOException e) { logger.error("optimize error", e); return new OperationResponse(e.getMessage(), ResultCodes.COMMON_ERROR); } catch (OutOfMemoryError e) { CloseUtil.close(indexWriter); logger.error("error of OOM", e); return new OperationResponse(e.getMessage(), ResultCodes.COMMON_ERROR); } return new OperationResponse(); }
protected void reopenSearcher() throws IOException { try { reopenLock.lock(); if (updateCount.get() > 0) { logger.info("proccessing reopen..."); IndexReader oldIndexReader = this.indexReader; IndexSearcher oldIndexSearcher = this.indexSearcher; IndexReader newIndexReader = IndexReader.openIfChanged(oldIndexReader); if (newIndexReader != null) { IndexSearcher newIndexSearcher = new IndexSearcher(newIndexReader); indexSearcher = newIndexSearcher; indexReader = newIndexReader; CloseUtil.close(oldIndexReader); CloseUtil.close(oldIndexSearcher); } updateCount.set(0); logger.info("proccess reopen finish."); } else { logger.info("no thing to reopen"); } } finally { reopenLock.unlock(); } }
@Override public OperationResponse commit() { try { if (logger.isDebugEnabled()) { logger.debug("commiting..."); } indexWriter.commit(); if (logger.isDebugEnabled()) { logger.debug("commit finish."); } reopenSearcher(); } catch (IOException e) { logger.error("commit error", e); return new OperationResponse(e.getMessage(), ResultCodes.COMMON_ERROR); } catch (OutOfMemoryError e) { CloseUtil.close(indexWriter); logger.error("error of OOM", e); // TODO reopen writer? return new OperationResponse(e.getMessage(), ResultCodes.COMMON_ERROR); } return new OperationResponse(); }
protected void open(Directory directory) throws IOException { IndexWriter newIndexWriter = null; IndexReader newIndexReader = null; IndexSearcher newIndexSearcher = null; if (logger.isDebugEnabled()) { logger.debug("opening directory..."); } try { IndexWriterConfig indexWriterConfig = new IndexWriterConfig(LuceneConfig.LUCENE_VERSION, schema.getAnalyzer()); newIndexWriter = new IndexWriter(directory, indexWriterConfig); newIndexReader = IndexReader.open(newIndexWriter, true); newIndexSearcher = new IndexSearcher(newIndexReader); synchronized (this) { IndexWriter oldIndexWriter = this.indexWriter; IndexSearcher oldIndexSearcher = this.indexSearcher; IndexReader oldIndexReader = this.indexReader; this.indexWriter = newIndexWriter; this.indexSearcher = newIndexSearcher; this.indexReader = newIndexReader; CloseUtil.close(oldIndexSearcher); CloseUtil.close(oldIndexReader); CloseUtil.close(oldIndexWriter); } if (logger.isDebugEnabled()) { logger.debug("open directory finish."); } } catch (IOException e) { logger.error("open directory error", e); CloseUtil.close(newIndexSearcher); CloseUtil.close(newIndexReader); CloseUtil.close(newIndexWriter); throw e; } }