Full Text Search with Hibernate Search
Posted by cagataycivici on March 6, 2007
Doing Full Text Search in your domain model might be tricky in the past but now with Hibernate Search based on Lucene it’s not a problem. If you’re already using hibernate annotations, configuration is very easy and can be done in a few steps. Firstly annotating the domain model with a few simple annotations and then adding the hibernate event listeners to the hibernate configuration sets up the whole thing. In our project with my teammate Sylvain Vieujot(co-author of Hibernate Search along with Emmanuel Bernard) we’ve integrated full text search to our forum module. Here’s a simplified example;
Configuration
By default lucene indexes are can be saved either on file system or in memory. This can be configured by the “hibernate.search.default.directory_provider” parameter. Instead of these directory providers, it’s possible to hold the indexes in database using the Compass’s extension called JDBCDirectoryProvider. In addition a couple of event listeners need to be configured.
hibernate.search.default.directory_provider=org.hibernate.search.store.FSDirectoryProvider
hibernate.search.default.indexDir=C:\LuceneIndexes
| hibernate-configuration> … … … <event type=”post-update” <listener class=”org.hibernate.search.event.FullTextIndexEventListener”/> </event> <event type=”post-insert” <listener class=”org.hibernate.search.event.FullTextIndexEventListener”/> </event> <event type=”post-delete” <listener class=”org.hibernate.search.event.FullTextIndexEventListener”/> </event> … … … </hibernate-configuration> |
Domain Model
The annotations are the glue to the lucene. Field, Index, Store, Boost they all refer to the core lucene api. By use of these hibernate search takes care of the underlying lucene search engine configuration.
|
Query
The coolest thing here is that, a lucene Query becomes a hibernate Query and you can use the regular stuff like list, scroll with it. In the example below Lucene Query Api is used, but it’s also possible to use Lucene’s Query Parser too, it does not matter since both can create a Lucene Query Object.
|
When the session factory is built, empty lucene directories per entity is created under the main lucene index. Using FullTextSession.index(Object entity) method, it’s possible to rebuild an index of an entity. With the Luke tool, you can browse the lucene indexes kept in filesystem, this’ll really help during development.

March 8, 2007 at 12:48 pm
You could also check out Compass to do this
March 9, 2007 at 12:32 am
Yes, we use compass jdbcdirectoryprovider to store the indexes in db. Anyway if filesystem will be used,I’d not like to have yet another dependency since this is already builtin feature of hibernate.
March 16, 2008 at 7:01 pm
I have an existing Spring + Hibernate web application. I haven’t used Hibernate Search (Lucene) before. I don’t want my index to reside on a local file directory. How can I configure the index (hibernate.search.default.indexDir) to be stored in the database instead?
May 5, 2008 at 11:45 am
Hi !
Thanks for this post. I’ve used Spring+Lucene integration and Hibernate DAO with some AOP stuff to implement full-text search. I guess my blog entry could be interesting for you Search with Spring Hibernate Lucene and Aspect Oriented Programming in action