The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Using Lucene with Hibernate

1 reply on 1 page. Most recent reply: Jul 23, 2008 1:43 PM by Mike Desjardins

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Using Lucene with Hibernate Posted: Jul 23, 2008 9:16 AM
Reply to this message Reply
Advertisement

Hibernate pioneered the mapping of structured object data to relational databases. A Hibernate sub-project, Hibernate Search, has for some time provided a similarly convenient way to combine structured data and unstructured text searches in the same API, and even inside the same transactional model.

Hibernate search incorporates the text-search capabilities of the Apache Lucene project into the Hibernate and JPA programming models. The solution works well whether the textual data is stored in database tables or externally to the database. Xinyu Liu's latest JavaWorld article, Introduction to Hibernate Search, introduces Hibernate Search, and provides a complete example of using the tool in conjunction with Spring 2.5. The advantage of this combination is that developers can specify all framework metadata with annotations, including metadata related to Hibernate Search:

Some database vendors do implement full-text search functions in their products as SQL extensions. To some degree, these proprietary functions are quite easy to use, but they compromise the portability of your applications at the database level. Besides, the features are no match for the user experience that Lucene offers, and under extreme conditions Lucene's performance is superior...

[For example, the analyzer architecture in Lucene leverages Java's built-in internationalization and localization capabilities, which makes full-text query available for various languages worldwide. Lucene delivers outstanding performance through some innovative techniques, such as an inverted index...

Auto-indexing automatically saves, updates, or deletes the Lucene Document object from index files whenever a corresponding JPA entity instance is persisted, updated, or deleted from the database through a persistence context (Hibernate Session/JPA EntityManager). This makes the entire indexing processes transparent to application developers. Manual indexing in Hibernate Search is only needed when index files are corrupted, or when there is existing data in the database to be indexed.

A key focus of Liu's article is the integration of Hibernate Search and rest of the JPA API, as provided by Hibernate's JPA persistence provider:

Several key factors allow Hibernate and Lucene to align well by nature. They both provide CRUD access to the underlying data storage. They both define an elementary operational data unit —the Entity (persistence model class) in Hibernate/JPA, and the Document in Lucene. And the same programming concepts coexist in Hibernate/JPA and Lucene -- deferred commit, filter, query expression, and query API are examples...

By mapping the application-specific persistence model classes to the Lucene Document class, Hibernate Search brings the power of Lucene full-text search to the persistence domain objects managed by Hibernate/JPA. The same persistence context (Hibernate Session/JPA EntityManager) is used for both domain-object persistence and Lucene indexing. Hibernate Search encloses Lucene indexing processes into the transaction contexts of Hibernate/JPA, and transparently manages the lifecycle of Lucene Document objects through the event handler mechanism of Hibernate Core. When auto-indexing is enabled, indexing processes become completely transparent to developers, and development around Hibernate Search thus becomes very easy.

Liu also points out that Hibernate Search requires textual data to search, and that many tools exist that can extract textual information from a wide array of sources:

Lucene indexing can't deal with any data type other than text strings; thus, all bean properties to be indexed must be converted to their string representations. A FieldBridge in Hibernate Search works as a data type converter in JSF or Spring that transforms any data type into text. The built-in FieldBridges take care of all the built-in Java data types... A wide variety of DocumentHandler implementations are available online for certain binary data types, including Word, Excel, PDF, HTML, and XML. It is nearly effortless to build custom FieldBridges out of the existing Lucene DocumentHandlers.

What do you think of Hibernate Search? What tools do you use to provide text-based searching in relational data sources?


Mike Desjardins

Posts: 1
Nickname: desjardins
Registered: Jul, 2008

Re: Using Lucene with Hibernate Posted: Jul 23, 2008 1:43 PM
Reply to this message Reply
I recently did some experimenting with Hibernate Search, and was absolutely amazed at how easy it was to set it up. Just a few configuration options and some annotations in the persistent classes were all that it took. Blatant plug (sorry) - I blogged about the experience here: http://mikedesjardins.us/blog/2008/07/twenty-minutes-with-hibernate-search.html

Flat View: This topic has 1 reply on 1 page
Topic: Using Lucene with Hibernate Previous Topic   Next Topic Topic: Google Open-Sources Data Serialization Tool

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use