Hibernate Search 监控和统计

发布    |       Hibernate Search

Hibernate Search 是一个库,通过自动索引实体,将 Hibernate ORM 与 Apache Lucene 或 Elasticsearch 集成,从而实现高级搜索功能:全文搜索、地理空间搜索、聚合等。更多信息,请参阅 hibernate.org 上的 Hibernate Search。

Emmanuel 在他之前的 搜索文章 中提到了 Hibernate search 3.3(最新版本 3.3.0.Beta1)中的新统计接口。我认为是时候更多地介绍它了。实际上,API 是自我解释的。

package org.hibernate.search.stat;
...
public interface Statistics {
    /**
     * Reset all statistics.
     */
    void clear();

    /**
     * Get global number of executed search queries
     *
     * @return search query execution count
     */
    long getSearchQueryExecutionCount();

    /**
     * Get the total search time in nanoseconds.
     */
    long getSearchQueryTotalTime();

    /**
     * Get the time in nanoseconds of the slowest search.
     */
    long getSearchQueryExecutionMaxTime();

    /**
     * Get the average search time in nanoseconds.
     */
    long getSearchQueryExecutionAvgTime();

    /**
     * Get the query string for the slowest query.
     */
    String getSearchQueryExecutionMaxTimeQueryString();

    /**
     * Get the total object loading in nanoseconds.
     */
    long getObjectLoadingTotalTime();

    /**
     * Get the time in nanoseconds for the slowest object load.
     */
    long getObjectLoadingExecutionMaxTime();

    /**
     * Get the average object loading time in nanoseconds.
     */
    long getObjectLoadingExecutionAvgTime();

    /**
     * Gets the total number of objects loaded
     */
    long getObjectsLoadedCount();

    /**
     * Are statistics logged
     */
    public boolean isStatisticsEnabled();

    /**
     * Enable statistics logs (this is a dynamic parameter)
     */
    public void setStatisticsEnabled(boolean b);

    /**
     * Returns the Hibernate Search version.
     *
     * @return the Hibernate Search version
     */
    String getSearchVersion();

    /**
     * Returns a list of all indexed classes.
     *
     * @return list of all indexed classes
     */
    Set<String> getIndexedClassNames();

    /**
     * Returns the number of documents for the given entity.
     *
     * @param entity the fqc of the entity
     *
     * @return number of documents for the specified entity name
     *
     * @throws IllegalArgumentException in case the entity name is not valid
     */
    int getNumberOfIndexedEntities(String entity);

    /**
     * Returns a map of all indexed entities and their document count in the index.
     *
     * @return a map of all indexed entities and their document count. The map key is the fqc of the entity and
     *         the map value is the document count.
     */
    Map<String, Integer> indexedEntitiesCount();
}

通过 SearchFactory.getStatistics() 访问统计信息。有关哪些类被索引以及索引中实体数量的信息始终可用。然而,只有当您的配置中设置了属性 hibernate.search.generate_statistics 时,才会收集查询和对象加载时间。我正在考虑引入一个额外的接口,以便更明显地分离这一点。你怎么看?

新的统计和监控功能并不止于此。您还可以通过 JMX 启用对统计信息的访问。设置属性 hibernate.search.jmx_enabled 将自动将 StatisticsInfoMBean 注册到 MBeanServer。在此 MBean 之上还有另外两个 MBean - IndexControlMBeanIndexingProgressMonitorMBean,它们是否可用取决于您的配置和应用程序的状态。

《IndexControlMBean》允许您为特定实体构建、优化和清理索引。索引通过批量索引API进行。此bean需要在JMX中注册的要求是,Hibernate SessionFactory通过`hibernate.session_factory_name`属性绑定到JNDI。有关如何配置JNDI的更多信息,请参阅Hibernate Core手册。《IndexControlMBean》API目前仅处于实验阶段。

最后但同样重要的是,《IndexingProgressMonitorMBean》。该MBean是《MassIndexerProgressMonitor》接口的实现。如果设置了`hibernate.search.jmx_enabled`并且使用了批量索引API,可以通过此MBean跟踪索引进度。在索引过程中,bean将绑定到JMX。一旦索引完成,MBean就不再可用。同样,此API目前仅处于实验阶段。

您认为这个新的监控和统计API有价值吗?您是否缺少任何功能?请告诉我们,并使用搜索论坛Jira建议新功能或报告错误。

祝您愉快!


返回顶部