FullTextEntityManager在部署war文件之前索引数据库中的所有表的问题

问题描述 投票:0回答:1

我在我的代码中使用hibernate复杂搜索来搜索相似匹配的类。为了实现这一点,我添加了FullTextEntityManager以在启动时进行初始化。现在,我的应用程序由大型实体和记录组成,使得索引过程极大地减慢了war文件的部署速度,并且大多数时候在从不同服务器上的db连接时导致数据库连接超时。

/*I have tried using this approach to see if will stop the indexing upon running the project, but the search algorithm never works thereafter because not results was returned*/
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
            fullTextEntityManager.createIndexer().optimizeAfterPurge(true);


//This is the code doing the indexing of the db records before deployment
 public void initializeHibernateSearch() {

        try {
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
            fullTextEntityManager.createIndexer().startAndWait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

请问如何才能使索引仅在我要执行相似性匹配的实体上进行,或者更快地加速以防止应用程序部署的可怕延迟

java spring-boot lucene hibernate-search
1个回答
0
投票

质量索引不应该经常执行:您需要在第一次部署时执行一次,然后每次以向后不兼容的方式更改映射(在现有实体上添加新字段,更改分析器)领域,...)。其他部署不需要重建索引。

因此,您不应将重建索引视为应用程序服务器启动的一部分,而应将其视为部署过程的一部分。就像您要查看的那样,例如,在数据库上运行资源密集型SQL脚本来更新架构。

话虽这么说,质量索引可能很长,具体取决于您需要索引的数据量。但绝对不应该导致数据库连接超时。关键是要正确配置重建索引。

在您的情况下,您应特别注意限制质量索引器使用的连接数。两个设置将发挥作用:typesToIndexInParallelthreadsToLoadObjects

例如:

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
fullTextEntityManager.createIndexer()
  .typesToIndexInParallel( 2 )
  .threadsToLoadObjects( 5 )
  .startAndWait();

这指示质量索引器并行索引2种类型,并使用5个并行线程来加载每种类型的对象。为此,您必须为每种类型添加一个线程以加载实体ID。由于每个线程都需要一个与数据库的连接,这意味着质量索引器将使用2 * (5 + 1) = 12连接。

默认值是1个并行类型,6个加载线程,意味着1 * (6 + 1) = 7连接。

如果您需要使用较少的连接,以便其他应用程序仍可以访问数据库,请将数据库接受的同时连接数提高到远远高于7的连接数,或者将质量索引器配置为使用较少的连接。然而,这意味着质量索引将需要更长时间。

您可以在section about Mass Indexing in the documentationhere for JDBC connections in particular找到更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.