2020-03-21

Hibernate Search - Indexing

Indexing

Hibernate Search will transparently index every entity persisted, updated or removed through Hibernate ORM. Thus this code would transparently populate your index:

 Using Hibernate ORM to persist data, and implicitly indexing it through Hibernate Search
// Not shown: get the entity manager and open a transaction

Author author = new Author();
author.setName( "John Doe" );

Book book = new Book();
book.setTitle( "Refactoring: Improving the Design of Existing Code" );
book.getAuthors().add( author );
author.getBooks().add( book );

entityManager.persist( author );
entityManager.persist( book );

// Not shown: commit the transaction and close the entity manager
By default, in particular when using the Elasticsearch backend, changes will not be visible right after the transaction is committed. A slight delay (by default one second) will be necessary for Elasticsearch to process the changes.

For that reason, if you modify entities in a transaction, and then a execute search query right after that transaction, the search results may not be consistent with the changes you just performed.

See Synchronization with the indexes for more information about this behavior and how to tune it.

However, keep in mind that data already present in your database when you add the Hibernate Search integration is unknown to Hibernate Search, and thus has to be indexed through a batch process. To that end, you can use the mass indexer API, as shown in the following code:

Using Hibernate Search MassIndexer API to manually (re)index the already persisted data

SearchSession searchSession = Search.session( entityManager ); 

MassIndexer indexer = searchSession.massIndexer( Book.class ) 
        .threadsToLoadObjects( 7 ); 

indexer.startAndWait(); 

Get a Hibernate Search session, called SearchSession, from the EntityManager.
Create an "indexer", passing the entity types you want to index. Pass no type to index all of them.
It is possible to set the number of threads to be used. For the complete option list see Reindexing large volumes of data with the MassIndexer.
Invoke the batch indexing process.

No comments:

Post a Comment