2

我在其中一个实体类上使用带有条件索引的 Hibernate Search。该@Indexed实体的注释指定了一个自定义拦截器,该拦截器可防止对处于特定状态的实例进行索引。

这按预期完美运行。但是,我注意到当我使用 aMassIndexer重新索引所有内容时,它会忽略该实体类上的自定义拦截器。我已经通过调试模式确认拦截器甚至从未被调用,并且该实体的所有实例都被索引,即使它们符合被跳过的标准。

我错过了什么吗?在休眠搜索中是否有一种方法可以在实体类上使用任何自定义拦截器来重新索引?

更新

我尝试使用以下代码片段切换到 Sanne 建议的“旧式”方法:

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.setFlushMode(FlushMode.MANUAL);
fullTextSession.setCacheMode(CacheMode.IGNORE);
fullTextSession.beginTransaction();

// Kill the current index
fullTextSession.purgeAll(MyEntity.class);
int batchSize = 10;
ScrollableResults results = fullTextSession.createCriteria(MyEntity.class)
        .setFetchSize(batchSize)
        .scroll(ScrollMode.FORWARD_ONLY);
int index = 0;
while(results.next()) {

    // Re-index entites in batches of 10, freeing up memory after each batch
    index++;
    fullTextSession.index(results.get(0));
    if (index % batchSize == 0) {
        fullTextSession.flushToIndexes();
        fullTextSession.clear();
    }
}
fullTextSession.getTransaction().commit();

但是,我看到的行为与MassIndexer. MyEntity不调用条件映射拦截器,并且所有MyEntity实例都被索引,无论它们是否应该被索引。

4

2 回答 2

2

不,没有:HSEARCH-1190

致力于它,欢迎贡献和测试者!

您可以使用仅向前滚动的旧式索引,直到修复:Using flushToIndexes()

于 2012-10-05T09:59:22.400 回答
0

我遇到了同样的问题。不幸的是,我在任何地方都没有找到好的解决方案。我尝试开发自己的解决方案,但我并不为此感到自豪。也许有人可以改进它。

int index = 0;
EntityIndexingInterceptor eii = 
        ((SearchFactoryIntegrator) fullTextSession.getSearchFactory())
        .getIndexBindingForEntity(MyEntity.class)
        .getEntityIndexingInterceptor();
while(results.next()) {
    if (eii.onAdd(results.get(0)) != IndexingOverride.SKIP){  // explit call interceptor
        index++;
        fullTextSession.index( results.get(0) ); 
        if (index % BATCH_SIZE == 0) {
            fullTextSession.flushToIndexes();
            fullTextSession.clear(); 
        }
    }
}
fullTextSession.flushToIndexes();

我正在寻找更简单的方法来触发拦截器但没有成功:(

顺便提一句。如果您查看 EntityIndexingInterceptor 声明,您可以看到:

//FIXME should we add onPurge and onIndex?
于 2013-01-02T10:32:51.890 回答