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

Problem with FullTextEntityManager indexing all the tables in the db before deployment of war file

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

/*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();
        }
    }

请问我怎样才能让索引只发生在我要执行相似性匹配的实体上,或者更确切地说加快速度以防止应用程序部署出现可怕的延迟

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

因此,您不应将重建索引视为应用程序服务器启动的一部分,而应将其视为部署过程的一部分。例如,您可能会在数据库中查看 运行 资源密集型 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 documentation, and here for JDBC connections in particular 中找到更多信息。