Showing posts with label lucene. Show all posts
Showing posts with label lucene. Show all posts

Friday, June 1, 2012

Lucene Wildcard Search On Multiple Fields Using Query Builder

booleanJunction.must(qb.keyword().wildcard().onField("title").andField("author").andField("description").matching(searchTerm).createQuery());

Where searchTerm has to have "*" in it to support partial matching.

ClassCastException Gotcha With Hibernate Search

When using Hibernate Search, you'd think the following line will limit the results to only those matching the provided entity class:
QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Cat.class).get();

Unfortunately, it doesn't. It won't just return Cats, if may even return Dogs if they are also indexed, which will in turn cause ClassCastExceptions.

To limit to only cats, you'd need to specify the entity class in this line:
FullTextQuery searchQuery = fullTextSession.createFullTextQuery(luceneQuery,Cat.class);

Disable Lucene With Hibernate Search

One issue with Hibernate search is that it is activated the moment the hibernate search JAR file is detected in the classpath. However, sometimes it's on the classpath for other reasons e.g. a hibernate data object that has hibernate search annotations is also on classpath.

To prevent hibernate search from being switched on, add the following line to the hibernate.cfg.xml file:
<property name="hibernate.search.autoregister_listeners">false</property>

Reference: http://docs.jboss.org/hibernate/search/3.2/reference/en/html_single/#search-configuration-event

Using Lucene With Hibernate

Lucene is a powerful java-based full-text search engine. If you are using Hibernate, it is possible to use Lucene easily and transparently with Hibernate taking care of telling Lucene to update the indexes etc. Lucene searches can also be wrapped in Hibernate searches.

To use Lucene with Hibernate, download Hibernate Search: http://www.hibernate.org/subprojects/search.html

A copy of the Lucene libraries is also provided.

Setup
By default, Hibernate Search will be enabled once Hibernate detects that it is on the classpath.

Any additional configuration can be done in the hibernate.cfg.xml file. E.g.

<property name="hibernate.search.default.directory_provider">filesystem</property>
<property name="hibernate.search.default.indexBase">/var/lucene/indexes</property>

These tell Lucene to use the filesystem based index directory and the folder to store the indexes.

Searchable Fields
To tell Hibernate to auto-index classes & fields, mark classes with the annotation "@Indexed", and fields with "@Field", and the primary key as "@DocumentId".

All subsequent updates to the table will be indexed (Note: prior entries, if any, will have to be manually indexed).

Manual Indexing
A one-time manual indexing can be done by calling:
FullTextSession fts = Search.getFullTextSession(session);
fts.createIndexer().startAndWait();

Query
FullTextSession fts = Search.getFullTextSession(session);
QueryBuilder qb = fts.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get();
org.apache.lucene.search.Query luceneSearchQuery = qb.keyword().onFields("author").matching(searchParam).createQuery();
FullTextQuery hibernateQuery = fts.createFullTextQuery(luceneSearchQuery);
List<Book> results = hibernateQuery.list(); // same paradigm as Hibernate querying

References
Hibernate Search Manual: http://docs.jboss.org/hibernate/stable/search/reference/en-US/html/index.html