In today's world every web application requires a search mechanism like Google. According to my research hibernate search is a great framework which can provide similar functionality. It can easily be integrated in any hibernate and JPA application. Behind the scenes it uses the apache lucene library to create a search index and uses the lucene api for searching. Hibernate search event handlers take care of index creation, update and deletion part as soon as entity added, updated and deleted. I will write all event handlers and libraries dependencies for the same later in this document. One more interesting thing about search mechanism is it provides managed entities after search.

Integration

At very first we need to update event handler configuration in persistence.xml.
persistence.xml

Annotations

@Indexed
Informs Hibernate Search that we want to create a full text index for this entity. In fact, once you run it, you'll see a directory with the fully qualified class name in the index storage directory you specified in persistence.xml.
@DocumentId
Informs Hibernate Search which value is the identifier for this document within its index. Normally Lucene would use URLs. These entities are in a database, so we don't have URLs, Instead we use @DocumentIds.
@Field(index=Index.TOKENIZED)
Informs Hibernate Search that this is a field we are interested in and it is a string which should be tokenized (split into words).
Entity Bean

Library Dependencies

Now we need to configure proper libraries. This document contain hibernate search 3.1.1 GA library dependencies. All these libraries are available in JBOSS repository at location.
Maven Dependencies

Implementation

Let’s move on to search implementation. Write a class to search document Id based on searched text from lucene indexes. This method has some parameters which are required for search query generation.
Fields
Fields on which search needs to be performed. In our case this can only be content. As this is only indexed field (i.e. Searchable field has to be indexed).
QueryString
Actual test to be searched from index.
QueryClass
Actual entity call on which search needs to be performed.
Projections
Selection criteria which needs to selected from the entity. By default actual entity is selected. If we want something special to be selected then it has to be part of index creation.
Query
Full text query is nothing but a sub class of persistence query so it will perform all the EJB QL/HQL/JPQL functions.