Indexing data in Hibernate Search

I just started integrating Hibernate Search with my Hibernate app. Data is indexed using a Hibernate session every time I start the server.

FullTextSession fullTextSession = Search.getFullTextSession(session); Transaction tx = fullTextSession.beginTransaction(); List books = session.createQuery("from Book as book").list(); for (Book book : books) { fullTextSession.index(book); } tx.commit(); //index is written at commit time 

This is very inconvenient, and the server takes 10 minutes. Am I doing it right?

I wrote a scheduler that will periodically update indexes. Will it update existing index entries automatically or create duplicate indexes?

+4
source share
3 answers

As stated in the search hibernate guide, section 3.6.1, if you use annotations (now the default), listeners that start indexing in the repository are registered by default:

Hibernate Search is turned on when using Hibernate Annotations or Hibernate EntityManager. If for some reason you need to disable it, set hibernate.search.autoregister_listeners to false.

An example of how to enable them manually:

  hibConfiguration.setListener("post-update", new FullTextIndexEventListener()); hibConfiguration.setListener("post-insert", new FullTextIndexEventListener()); hibConfiguration.setListener("post-delete", new FullTextIndexEventListener()); 

All you have to do is annotate the objects you want to index with

 @Indexed(index = "fulltext") 

and then make a fine-grained annotation in the fields as described in the user guide.

Thus, you should not start indexing manually during storage, do not restart indexing when the application starts, unless you have objects that were saved before indexing.

You may experience performance issues when storing an object that has an "attachment", and therefore you index it in the same transaction area that stores the object. See here:

Hibernate Search and offline text extraction

for a solution that solves this problem.

+10
source

If you use FSDirectoryProvider (which is the default), the Lucene index is saved to disk. This means that there is no need to be indexed at very startup. If you have an existing database, of course you want to create an initial index using the fullTextSession.index () function. However, this should not be at application startup. Consider exposing some kind of trigger URL or admin interface. Once you have the starting index, I would recommend using automatic indexing. This means that the Lucene index is automatically updated if books are created / updated / deleted. Auto indexing should also be enabled by default.

I recommend that you familiarize yourself with the automatic and manual indexing sections in the online manual - http://docs.jboss.org/hibernate/stable/search/reference/en/html_single

- Hardy

+1
source

I am currently using Hibernate Search automatic indexing with JPA, and it works very well. To create your indexes first, you can simply call the following:

  FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); try { fullTextEntityManager.createIndexer().startAndWait(); } catch (InterruptedException e) { // Exception handling } 

where "entityManager" is just javax.persistence.EntityManager. The above will index all fields marked with @Field for all objects marked as @Indexed.

Then, while you are doing all your updates, etc., through the entity manager, indexes are automatically updated. Then you can perform the search as usual, but do not forget to create your EntityManager for each search (you can use EntityManagerFactory for this).

0
source

All Articles