在Hibernate Search中索引数据

Sha*_*shi 4 java hibernate-search

我刚开始将Hibernate Search与我的Hibernate应用程序集成.每次启动服务器时,都会使用Hibernate Session对数据进行索引.

FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();

List books = session.createQuery("from Book as book").list();
for (Book book : books) {
    fullTextSession.index(book);
}

tx.commit(); //index is written at commit time     
Run Code Online (Sandbox Code Playgroud)

它非常尴尬,服务器需要10分钟才能启动.我是以正确的方式做到这一点的吗?

我写了一个调度程序,它会定期更新索引.这会自动更新现有索引条目,还是创建重复索引?

Pie*_*lli 10

正如Hibernate搜索指南的第3.6.1节所述,如果您使用注释(现在是默认值),默认情况下会注册在商店上启动索引的侦听器:

使用Hibernate Annotations或Hibernate EntityManager时,Hibernate Search开箱即用.如果由于某种原因需要禁用它,请将hibernate.search.autoregister_listeners设置为false.

关于如何手动打开它们的示例:

 hibConfiguration.setListener("post-update", new FullTextIndexEventListener());
 hibConfiguration.setListener("post-insert", new FullTextIndexEventListener());
 hibConfiguration.setListener("post-delete", new FullTextIndexEventListener());
Run Code Online (Sandbox Code Playgroud)

您需要做的就是注释要使用索引编制索引的实体

@Indexed(index = "fulltext")
Run Code Online (Sandbox Code Playgroud)

注释,然后在字段上执行细粒度注释,如用户指南中所述.

因此,您既不应该在存储时手动启动索引,也不应该在应用程序启动时重新启动索引,除非您在启用索引之前已经存储了实体.

当您存储一个具有"附件"的对象时,您可能会遇到性能问题,因此您要在存储该实体的事务的同一范围内对其进行索引.看这里:

Hibernate搜索和离线文本提取

找到解决这个问题的解决方案.