java 在 Hibernate Search 中索引数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/934322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:26:01  来源:igfitidea点击:

Indexing data in Hibernate Search

javahibernate-search

提问by Shashi

I just started integrating Hibernate Search with my Hibernate application. The data is indexed by using Hibernate Session every time I start the server.

我刚刚开始将 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     

It is very awkward and the server takes 10 minutes to start. Am I doing the this in right way?

很尴尬,服务器需要10分钟才能启动。我是否以正确的方式做这件事?

I wrote a scheduler which will update the indexes periodically. Will this update the existing index entries automatically, or create duplicate indices?

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

回答by Pietro Polsinelli

As detailed in the Hibernate Search guide, section 3.6.1, if you are using annotations (by now the default), the listeners which launch indexing on store are registered by default:

如 Hibernate Search 指南第 3.6.1 节所述,如果您使用注解(现在是默认设置),默认情况下会注册在 store 上启动索引的侦听器:

Hibernate Search is enabled out of the box when using Hibernate Annotations or Hibernate EntityManager. If, for some reason you need to disable it, set hibernate.search.autoregister_listeners to false.

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

An example on how to turn them on by hand:

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

 hibConfiguration.setListener("post-update", new FullTextIndexEventListener());
 hibConfiguration.setListener("post-insert", new FullTextIndexEventListener());
 hibConfiguration.setListener("post-delete", new FullTextIndexEventListener());

All you need to do is annotate the entities which you want to be indexed with the

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

@Indexed(index = "fulltext")

annotation, and then do the fine-grained annotation on the fields, as detailed in the user guide.

注释,然后按照用户指南中的详细说明对字段进行细粒度注释。

So you should neither launch indexing by hand when storing, neither relaunch indexing whae the application starts, unless you have entities which have been stored before indexing was enabled.

因此,您不应在存储时手动启动索引,也不应在应用程序启动时重新启动索引,除非您有在启用索引之前已存储的实体。

You may get performance problems when you are storing an object which say has an "attachment" and so you are indexing that in the same scope of the transaction which is storing the entity. See here:

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

Hibernate Search and offline text extraction

休眠搜索和离线文本提取

for a solution that solves this problem.

对于解决此问题的解决方案。

回答by Hardy

Provided you are using a FSDirectoryProvider (which is the default) the Lucene index is persisted on disk. This means there is no need to index on very startup. If you have existing database you want of course to create an initial index using the fullTextSession.index() functionality. However, this should not be on application startup. Consider exposing some sort of trigger url, or admin interface. Once you have the initial index I would recommend to use automatic indexing. This means that the Lucene index gets automatically updated if a books get created/updated/deleted. Automatic indexing should also be enabled by default.

如果您使用的是 FSDirectoryProvider(这是默认设置),Lucene 索引将保留在磁盘上。这意味着不需要在启动时建立索引。如果您有现有的数据库,您当然希望使用 fullTextSession.index() 功能创建初始索引。但是,这不应在应用程序启动时进行。考虑公开某种触发器网址或管理界面。一旦你有了初始索引,我会建议使用自动索引。这意味着如果书籍被创建/更新/删除,Lucene 索引会自动更新。默认情况下也应启用自动索引。

I recommend you refer to the automatic and manual indexing sections in the online manual - http://docs.jboss.org/hibernate/stable/search/reference/en/html_single

我建议您参考在线手册中的自动和手动索引部分 - http://docs.jboss.org/hibernate/stable/search/reference/en/html_single

--Hardy

——哈代

回答by brent777

I currently use Hibernate Search's automatic indexing with JPA and it works really well. To create your indexes initially you can just call the following:

我目前在 JPA 中使用 Hibernate Search 的自动索引,并且效果很好。要最初创建索引,您只需调用以下命令:

    FullTextEntityManager fullTextEntityManager = 
                    Search.getFullTextEntityManager(entityManager);

    try {
       fullTextEntityManager.createIndexer().startAndWait();
    } catch (InterruptedException e) {
       // Exception handling
    }

where "entityManager" is just a javax.persistence.EntityManager. The above will index all fields marked with @Field for all entities marked as @Indexed.

其中“entityManager”只是一个 javax.persistence.EntityManager。以上将为所有标记为@Indexed 的实体索引所有标记为@Field 的字段。

Then as long as you do all your updates, etc, through the entity manager the indexes are automatically updated. You can then search as per usual but be sure to recreate your EntityManager on each search (you can use the EntityManagerFactory to do so).

然后,只要您通过实体管理器进行所有更新等,索引就会自动更新。然后您可以像往常一样搜索,但一定要在每次搜索时重新创建您的 EntityManager(您可以使用 EntityManagerFactory 这样做)。