如何在 GUI 中获取 Hibernate Lucene 索引创建的统计信息?

How to get the statistics of Hibernate Lucene Index creation in GUI?

我开发了一个管理面板,用于在创建 MassIndexer 时显示 Hibernate Lucene 索引统计信息,当我的应用程序在维护 mode.I 时在文档中发现我们可以使用 SearchFactory.getStatistics() 但我没有得到任何统计信息,下面是我的代码片段

Session session = sessionFactory.openSession();
        boolean flag=false;
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        SearchFactory searchFactory = Search.getFullTextSession(session).getSearchFactory();
fullTextSession
             .createIndexer(ABC.class)
             .typesToIndexInParallel( 2 )
             .batchSizeToLoadObjects( 25 )
             .cacheMode( CacheMode.IGNORE )
             .threadsToLoadObjects( 5 )
             .idFetchSize( 150 )
             .progressMonitor(new SimpleIndexingProgressMonitor()) //a MassIndexerProgressMonitor implementation
             .startAndWait();
            Statistics statistics2 = searchFactory.getStatistics();
            session.clear();

我在下面提到link我们可以为索引创建提供GUI[Can we create Lucene indexes only once at for initial set up only?

Statistics 对象收集有关 Hibernate Search 正常使用情况的统计信息。

要收集有关 Mass Indexer 进度的数据,您应该插入自己的 org.hibernate.search.batchindexing.MassIndexerProgressMonitor

MassIndexerProgressMonitor 是一个接口,因此您可以实现自己的接口,或者扩展 SimpleIndexingProgressMonitor 并根据需要覆盖一些方法。

MassIndexerProgressMonitor monitor = new CustomIndexerProgressMonitor();
fullTextSession.createIndexer(ABC.class)
            .progressMonitor(monitor)
            .start();
// Now your custom index progress monitor will receive notifications about the progress

通过收集有关自定义 MassIndexerProgressMonitor 实施的数据,您可以实施进度条或类似的东西。

  • 您已经知道它将索引哪些类型:您告诉他的类型(在上面的示例中,class ABC 及其子classes 的所有对象.
  • 方法 MassIndexerProgressMonitor.addToTotalCount(long) 将被调用多次 - 从技术上讲是针对每个子类型 - 但所有调用都发生在进程 "at the beginning" 中,因此您可以将其用作对总调用次数的估计要索引的元素(您的“100%”)。
  • 方法MassIndexerProgressMonitor.documentsBuilt(int)会让你知道正在取得的进展:这将被多次调用,你必须在你的实施中保留一个总计数器来跟踪总进度 - 从这里你可以估计你走了多远。
  • 您可以将这些指标与一些时间跟踪相结合,以估计完成需要多长时间。

请记住,当索引很小并且适合您的 ram 时,索引速度会更快一些,因此在进行过程中它往往会慢一点。如果你给出一个乐观的估计然后放慢速度,人们通常会很生气,而如果你略微高估然后实际上表现稍好,他们会很高兴,所以你可能想要将估计时间的计算调整为略微悲观 - 如何很大程度上取决于您的典型索引大小和磁盘速度,因此请尝试一下。