Compass, JPA & Spring, or How I Added Searching in a Few Hours

I just added full search support to a web application in about 3 hours.

That’s all the data model objects automatically indexed, a proper query language for searching, and mapping between the search result and model objects so dealing with results is easy. All in a few hours – not bad going for an afternoon’s work I feel.

How did I achieve this remarkable feat? Three tools:

  • Compass – search framework
  • JPA – object relational mapping
  • Spring – application framework

The combination of these three tools makes it remarkably simple to add searching to an application.

Compass is where is search magic happens – it presents a straightforward but powerful interface to the full Lucene search engine. It makes indexing and searching trivial. Domain objects are made searchable by annotating the classes to indicate how they should behave with searching. Then Compass is able to index these classes and convert between results in a search and they objects they represent.

Things become even simpler using JPA as Compass can be configured to index any changes made to managed objects. Once setup correctly everything “just works” – indexing happens auto-magically, so there’s no code to write.

The Compass support for Spring rounds out the whole thing by making it easy to integrate to an application. The DAO support for searching is much like querying a database so it fits into the existing model for applications without any problems.

I’m not going to go through my configuration step by step, Compass has excellent documentation and the distribution includes a search enabled version of the Spring ‘PetClinic’ sample application which provides a useful reference. If I figured it out it can’t be that hard now can it?

When its this easy to do why wouldn’t you make your application searchable?