Hibernate Search对SearchFactory optimize的调用不会立即调用

时间:2014-09-20 01:02:34

标签: hibernate-search

我试图调用SearchFactory optimize来运行预定的索引维护作业(压缩段 - 应用程序是写入密集型的)。但是在我关闭Tomcat之前似乎没有立即调用它。我的代码就像这样调用。

public synchronized void optimizeIndexes() {
    getFullTextEntityManager().flushToIndexes(); //apply any changes before optimizing
    getFullTextEntityManager().getSearchFactory().optimize();
    logger.info("[Lucene] optimization has performed on all the indexes...");
}

1 个答案:

答案 0 :(得分:0)

我通过从HSearch后端借用IndexWriter来解决这个问题。

private synchronized void optimizeBareMetal() {
    try {
        LuceneBackendQueueProcessor backend = (LuceneBackendQueueProcessor) getIndexManager().getBackendQueueProcessor();
        LuceneBackendResources resources = backend.getIndexResources();
        AbstractWorkspaceImpl workspace = resources.getWorkspace();
        IndexWriter indexWriter = workspace.getIndexWriter();
        indexWriter.forceMerge(1, true);
        indexWriter.commit();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private synchronized DirectoryBasedIndexManager getIndexManager() {
    SearchFactoryImplementor searchFactory = (SearchFactoryImplementor) getFullTextEntityManager().getSearchFactory();
    IndexManagerHolder indexManagerHolder = searchFactory.getIndexManagerHolder();
    return (DirectoryBasedIndexManager) indexManagerHolder.getIndexManager(getEntityClass().getName());
}