Hibernate Search is a library that integrates Hibernate ORM with Apache Lucene or Elasticsearch by automatically indexing entities, enabling advanced search functionality: full-text, geospatial, aggregations and more. For more information, see Hibernate Search on hibernate.org.

Emmanuel mentioned in his previous Search post the new Statistics interface which is new in Hibernate search 3.3 (latest version 3.3.0.Beta1). I thought it is time to write a little bit more about it. The API is actually self-explanatory:

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

Access to the statistics is via SearchFactory.getStatistics(). The information about which classes are indexed and how many entities are in the index will always be available. However, the query and object loading timings will only be collected if the property hibernate.search.generate_statistics is set in your configuration. I am thinking about introducing an additional interface in order to make this separation more obvious. WDYT?

The new statistic and monitoring functionality does not end here. You can also enable access to the statistics via JMX. Setting the property hibernate.search.jmx_enabled will automatically register the StatisticsInfoMBean with the MBeanServer. On top of this MBean there are two more MBeans - IndexControlMBean and IndexingProgressMonitorMBean - which will or will not be available depending on your configuration and the state of the application.

The IndexControlMBean allows you to build, optimize and purge the index for a given entity. Indexing occurs via the mass indexing API. A requirement for this bean to be registered in JMX is, that the Hibernate SessionFactory is bound to JNDI via the hibernate.session_factory_name property. Refer to the Hibernate Core manual for more information on how to configure JNDI. The IndexControlMBean API are for now just experimental.

Last but not least, the IndexingProgressMonitorMBean. This MBean is an implementation of the MassIndexerProgressMonitor interface. If hibernate.search.jmx_enabled is set and the mass indexer API is used the indexing progress can be followed via this MBean. The bean will only be bound to JMX while indexing is in progress. Once indexing is completed the MBean is not longer available. Again, this API is for now experimental.

Do you think this new monitoring and statistic API it is valuable? Are you missing any functionality? Let us know and use the Search forum or Jira to suggest new features or to report a bug.

Enjoy!


Back to top