Keeping database and Lucene index in sync is easily done using the new Lucene-Hibernate integration.

Just place the default org.hibernate.search.event.FullTextIndexEventListener on Hibernate’s lifecycle methods, configure the Lucene annotations in classes you want to search and you’re ready to go. For custom operations such as persisting custom classes, you can write bridges where you can create custom fields in the index.

Save/update/deletes of your hibernate objects will propagate in Lucene index. A full reindex can be implemented as well using a custom the new FullTextSession class.

Everything is great on the keeping in sync part, but how about doing actual searches?

The intent was to wrap actual Lucene queries and have them transformed into Hibernate queries, empowering the API user with all the API Hibernate provides for query manipulation (eg. pagination). Problem is getting a hold of the actual Lucene index is no longer possible. All that nicely created and maintained in sync index cannot be used with real Lucene queries, using Lucene API to extract the information.

A simple problem was using Lucene to do a Sort for me, turns out that current API does not provide that. As I understand, 3.3.0 will allow direct access to Lucene Directory (see here). To get around that you can use something similar to SearchDAO.java using the same index access as Lucene-Hibernate integration is using.

3 responses to “Hibernate Annotations 3.2.1 – Lucene sort

  1. It’s seems sorting is an obvious need!
    I’m glad I found this blog entry. My latest project requires sorting.

    However, how do I use the SearchDAO class?

    It looks like I’ll need to grab a hold of a FullTextIndexEventListener, not to mention the DocMapper.

    And I’m not even sure where SearchDAO fits in the whole system? Is it meant to be a standalone object I instantiate, or do I need to have it configured in somewhere?

    thanks,
    Fi
    Broker-Realtor and Freelance Coder :)

  2. FullTextIndexEventListener is part of Hibernate-Lucene integration (org.hibernate.search.event.FullTextIndexEventListener).

    DocMapper is an interface you will have to implement to do your own Lucene-to-YourModel mappings:

    public interface DocMapper {
    public Object mapDocument(Document doc);
    }

    The easiest way to use SearchDAO is by using Spring Framework. Otherwise you can instantiate it and set the provided FullTextIndexEventListener.

  3. Hi Ioan,

    Thanks for your reply.

    I am using Spring via code generation with AndroMDA.

    I’ll see if I can link in the SearchDao through the configurations somehow. But in the meantime, it might be easier (and less “global”) for me to just instantiate the SearchDao myself.

    I found that org.hibernate.search.util.ContextHelper has a static method to get an instance of FullTextIndexEventListener, hopefully that method will “just work”.

    For simplicity, I’ll probably just inline that code into my own and see if that works.

    thanks for your help,
    Fi

Leave a Comment:

Your email address will not be published. Required fields are marked *