6.2. パージ
データベースから特定のタイプのエンティティを物理的に削除せずに Lucene インデックスから削除できます。この操作はパージと呼ばれ、
FullTextSession から実行されます。
例6.3 インデックスあkらエンティティの特定のインスタンスをパージ
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
for (Customer customer : customers) {
fullTextSession.purge( Customer.class, customer.getId() );
}
tx.commit(); //index are written at commit time
パージにより、Lucene インデックスから特定の id を持つエンティティが削除されますが、データベースは変更されません。
特定のタイプのすべてのエンティティを削除する必要がある場合は、
purgeAll メソッドを使用できます。この操作はパラメータおよびすべてのサブタイプとして渡されたタイプのすべてのエンティティを削除します。
例6.4 インデックスからエンティティのすべてのインスタンスをパージ
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
fullTextSession.purgeAll( Customer.class );
//optionally optimize the index
//fullTextSession.getSearchFactory().optimize( Customer.class );
tx.commit(); //index are written at commit time
このような操作後にインデックスを最適化することが推奨されます。
注記
メソッド
index、purge、および purgeAll はFullTextEntityManager でも利用可能です。