Hibernate.orgCommunity Documentation

Chapter 7. Index Optimization

7.1. Automatic optimization
7.2. Manual optimization
7.3. Adjusting optimization

From time to time, the Lucene index needs to be optimized. The process is essentially a defragmentation. Until an optimization is triggered Lucene only marks deleted documents as such, no physical deletions are applied. During the optimization process the deletions will be applied which also effects the number of files in the Lucene Directory.

Optimizing the Lucene index speeds up searches but has no effect on the indexation (update) performance. During an optimization, searches can be performed, but will most likely be slowed down. All index updates will be stopped. It is recommended to schedule optimization:

When using a MassIndexer (see Section 6.3.2, “Using a MassIndexer”) it will optimize involved indexes by default at the start and at the end of processing; you can change this behavior by using respectively MassIndexer.optimizeAfterPurge and MassIndexer.optimizeOnFinish.

Hibernate Search can automatically optimize an index after:

The configuration for automatic index optimization can be defined on a global level or per index:

An optimization will be triggered to the Animal index as soon as either:

  • the number of additions and deletions reaches 1000

  • the number of transactions reaches 50 (hibernate.search.Animal.optimizer.transaction_limit.max having priority over hibernate.search.default.optimizer.transaction_limit.max)

If none of these parameters are defined, no optimization is processed automatically.

You can programmatically optimize (defragment) a Lucene index from Hibernate Search through the SearchFactory:

The first example optimizes the Lucene index holding Orders; the second, optimizes all indexes.


searchFactory.optimize() has no effect on a JMS backend. You must apply the optimize operation on the Master node.

Apache Lucene has a few parameters to influence how optimization is performed. Hibernate Search exposes those parameters.

Further index optimization parameters include:

See Section 3.10, “Tuning Lucene indexing performance” for more details.