Ce contenu n'est pas disponible dans la langue sélectionnée.

Chapter 6. Manual indexing


6.1. Indexing

It is sometimes useful to index an entity even if this entity is not inserted or updated to the database. This is for example the case when you want to build your index for the first time. FullTextSession.index() allows you to do so.

Example 6.1. Indexing an entity via FullTextSession.index()

FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
for (Customer customer : customers) {
    fullTextSession.index(customer);
}
tx.commit(); //index are written at commit time
Copy to Clipboard Toggle word wrap
For maximum efficiency, Hibernate Search batches index operations and executes them at commit time. If you expect to index a lot of data, however, you need to be careful about memory consumption since all documents are kept in a queue until the transaction commit. You can potentially face an OutOfMemoryException. To avoid this exception, you can use fullTextSession.flushToIndexes(). Every time fullTextSession.flushToIndexes() is called (or if the transaction is committed), the batch queue is processed (freeing memory) applying all index changes. Be aware that once flushed changes cannot be rolled back.

Note

hibernate.search.worker.batch_size has been deprecated in favor of this explicit API which provides better control
Other parameters which also can affect indexing time and memory consumption are:
  • hibernate.search.[default|<indexname>].indexwriter.[batch|transaction].max_buffered_docs
  • hibernate.search.[default|<indexname>].indexwriter.[batch|transaction].max_field_length
  • hibernate.search.[default|<indexname>].indexwriter.[batch|transaction].max_merge_docs
  • hibernate.search.[default|<indexname>].indexwriter.[batch|transaction].merge_factor
  • hibernate.search.[default|<indexname>].indexwriter.[batch|transaction].ram_buffer_size
  • hibernate.search.[default|<indexname>].indexwriter.[batch|transaction].term_index_interval
These parameters are Lucene specific and Hibernate Search is just passing these parameters through - see Section 3.8, “Tuning Lucene indexing performance” for more details.

Example 6.2. Efficiently indexing a given class (useful for index (re)initialization)

fullTextSession.setFlushMode(FlushMode.MANUAL);
fullTextSession.setCacheMode(CacheMode.IGNORE);
transaction = fullTextSession.beginTransaction();
//Scrollable results will avoid loading too many objects in memory
ScrollableResults results = fullTextSession.createCriteria( Email.class )
    .setFetchSize(BATCH_SIZE)
    .scroll( ScrollMode.FORWARD_ONLY );
int index = 0;
while( results.next() ) {
    index++;
    fullTextSession.index( results.get(0) ); //index each element
    if (index % BATCH_SIZE == 0) {
        fullTextSession.flushToIndexes(); //apply changes to indexes
        fullTextSession.clear(); //clear since the queue is processed
    }
}
transaction.commit();
Copy to Clipboard Toggle word wrap
Try to use a batch size that guarantees that your application will not run out of memory.
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat