7.6. 手动索引更改
当 Hibernate Core 对数据库应用更改时,Hibernate Search 会检测到这些更改并自动更新索引(除非禁用了 EventListeners)。有时会在不使用 Hibernate 的情况下对数据库进行更改,因为备份恢复或者您的数据会受到影响。在这些情况下,Hibernate Search 会公开 Manual Index API 来显式更新或删除索引中的单个实体,重建整个数据库的索引,或删除对特定类型的所有引用。
所有这些方法仅影响 Lucene index,不会对数据库应用任何更改。
7.6.1. 将实例添加到索引 复制链接链接已复制到粘贴板!
使用 FullTextSession.index(T 实体)
,您可以直接添加或更新特定对象实例到索引。如果此实体已经索引,则会更新索引。对索引的更改仅在事务提交时应用。
使用 FullTextSession.index(T 实体)
直接将对象或实例添加到索引中。索引会在实体索引时更新。Infinispan Query 在事务提交过程中对索引应用更改。
示例:使用 FullTextSession.index(T 实体)来索引实体.
FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction tx = fullTextSession.beginTransaction(); Object customer = fullTextSession.load( Customer.class, 8 ); fullTextSession.index(customer); tx.commit(); //index only updated at commit time
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
Object customer = fullTextSession.load( Customer.class, 8 );
fullTextSession.index(customer);
tx.commit(); //index only updated at commit time
如果您想要为类型或者所有索引类型添加所有实例,推荐的方法是使用 MassIndexer:请参阅 以了解更多详细信息。
使用 MassIndexer 添加类型(或所有索引类型)的所有实例。如需更多信息,请参阅使用 MassIndexer。