Take the 2-minute tour ×

In one of my applications, I am to execute a search on multiple fields/columns. Its an Advanced Search and there are over 20 fields using which a user can search for results. For example, a user can search for bookings based on

  1. Booking Id
  2. Passenger Name
  3. Passenger Age
  4. From Location
  5. To Location
  6. Booking Status
  7. Name of the Airline

and 13 such fields.

I am trying to figure out if

  1. Hibernate Search can and should be used here? If so, how? I was unable to find an example for such a complex search using Hibernate Search.

  2. Instead of Hibernate search, I can simply use Hibernate and maybe design a multi-threaded search depending in the number of parameters. Would that be a good idea?

  3. Is it possible to use Hibernate Filters here?

Can someone please provide inputs or reference links?

share|improve this question
Are you expecting the user to enter their search criteria into one field and then test that against multiple columns? Or separate search fields for each column? –  James DW May 20 '12 at 21:57
Its separate fields for each column. –  SB. May 21 '12 at 3:12
add comment

3 Answers

up vote 1 down vote accepted

For these kinds of queries, I generally use a Criteria query with a form object. I then check for null in each passed form field, and if not null, then add another Restriction into the query, using that field. Using a Criteria query keeps the Java code very clean and eliminates messy string concatenation. An example for your case:

// Form object
public class bookingSearchForm {
    private String bookingId;
    public getBookingId()...
    public setBookingId()...

// Hibernate
Criteria criteria = getSession().createCriteria(Booking.class);
if(form.getBookingId() != null) {
    criteria.add(Restrictions.eq("bookingId", form.getBookingId()));
share|improve this answer
Hi @Aaron, thanks for the reply. My problem is that the data is spread across about 25 tables. So say if I am using Criteria, it would be a huge statement with Joins. This is something I want to avoid to have a clean implementation. what you are proposing works perfectly if I have say 5-6 fields to search on. –  SB. Apr 27 '12 at 4:46
If you have the Hibernate entities set up with relationships, then Hibernate will take care of all of those joins for you, and you will be able to use the above strategy on those other tables. Think in Hibernate, not SQL, and let the tool do the work for you. –  atrain Apr 27 '12 at 12:42
I am not sure if I understand you correctly. Let me give you an example, I am to search for "Bookings" where the "Trip Type" is "one-way". The Trip Type is embedded in an Class which is 6 level below the 'Booking' class. So I would actually have to search on the Trip Type and not on the booking class. Now there can be 20 such parameters on which I would search which are embedded at various levels from the Booking class. If I have to do this using a Criteria, I would have to join all the tables at these levels which I think is very cumbersome. –  SB. Apr 27 '12 at 17:40
add comment

I think your decision should be based on the requirement for capacity. Ordinary Hibernate query might be sufficient for searching on those many fields.

Hibernate Search will help if you want to get the search query fast for large amount of data, using reverse index. What you should do is to load your database with the capacity you expect in the next 5 years for example. If your ordinary Hibernate query is considered acceptable I think you should stick with ordinary Hibernate query.

You can introduce the Hibernate Search in later stage as well.

EDIT: You may opt not to use Hibernte Search, go down to the lower level and use Apache Lucene instead. You can generate your own Apache Lucene index. This way you don't have to worry about which field in order to find records because you will have full control on how the tokenizing and indexing process. You can store the row and column name as part of the search result if you choose this way.

share|improve this answer
Thanks for your reply @Daniel. My concern here is the amount of complex code that would have to be written to generate the query. I am searching from over 25 tables and the number of joins required to be written manually would be very error prone. –  SB. May 24 '12 at 19:25
Worry about complex code? Go down one level to Apache Lucene. You don't have to create Hibernate query, instead query to Lucene index that you create yourself. The only drawback is that you need to understand the underlying Lucene technology. –  Daniel Baktiar Oct 4 '12 at 13:45
add comment

Yes, Hibernate Search is going to be very useful in such a case. You can use filters, but Hibernate Search has filters as well.

share|improve this answer
add comment

Your Answer


