7.6.2. 从索引中删除实例
可以从 Lucene 索引中删除给定类型的实体或所有实体,而无需将它们从数据库中物理删除。此操作名为清除,也通过 FullTextSession 进行。
清除操作允许从 Lucene 索引中删除单个实体或指定类型的所有实体,而无需将它们从数据库中物理删除。此操作使用 FullTextSession 来执行。
示例:从索引中清除实体的特定实例
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
for (Customer customer : customers) {
fullTextSession.purgeAll( Customer.class );
//optionally optimize the index
//fullTextSession.getSearchFactory().optimize( Customer.class );
tx.commit(); //index is updated at commit time
建议在此类操作后优化索引。
注意
FullTextEntityManager 上也提供方法索引、清除和清除All。
注意
所有手动索引方法(index、purge 和 purgeAll)都只会影响索引,而不是数据库,但是在事务成功提交或您使用 flushToIndexes 之前不会应用它们。