There’s been a some talk on the Grails list from different folk that are having a go at implementing a fulltext Search for their Grails application. Maurice has done a sensational job with integrating Compass, and looks like a fantastic platform for a future integrated Grails search capability.

Recently I’ve been using raw Lucene, and I have my hit hitlighting happening (including … ellipses for joining multiple hits in a single document), along with some snazzy search paging, and general Google-like goodness. Think of it as a “Cheap and Cheerful” search capability you can implement in a hurry on your current Grails app.

Here’s a grab of the beast in action. I’ve slurped in the RSS feeds of recent groovy and grails posts from Nabble to have some reasonable test data, but the harness lets you add your own documents to search against.

groogle Search Screen

My approach, while nowhere near as feature-packed or snazzy, just uses standard Grails domain classes, on which you add an “indexedFields” method. You then need to call index(), reindex(), and unindex() in your Controller classes, but even with those changes, you should be able to add full text search to your app in about 15 minutes. I thought I’d post up my progress so far which I hope to turn into a simple grails plugin for indexing - Groogle.

Implementing the search consists of a number of components that will be familiar to Grails developers:

  • SearchTagLib to give you handy g:searchBox, g:searchResults, and g:searchCrumbs
  • SearchService which does the heavy lifting of indexing, reindex, unindexing and searching your Grails documents using Lucene indexes

So how can you implement it for your own application? Well it’d be best to use a Groogle plugin, but we’re not there yet, so you’ll have to download the demo app and pull it apart yourself for now. Or wait a couple of weeks for a real plugin :-).

In general terms, what you’ll need to do is:

  1. Put a copy of lucene-core-2.0.0.jar and lucerne-highlight-2.0.0.jar into your project’s lib directory

  2. Add a indexedFields() method to domain classes that you want to be searchable. This method should return a map of the field names and values that you want to index.

  3. Add the SearchService to your service classes

  4. Copy the supporting Groovy classes out of /src/groovy into your own project’s /src/groovy.

  5. Inject the SearchService into any controller that creates or updates your domain class

  6. When calling save() or updating your domain class, make a call to searchService.index(myObj) or searchService.reindex(myObj) or searchService.unindex(myObj) as appropriate.

  7. Copy the SearchController and search.gsp into appropriate places in your project. Edit the g:searchBox tag in search.jsp to include the fields you want to search on.

As I said, this is the stuff that plugins do oh-so-well… You might be better of waiting a week or two for the plugin. :-)

Of course, you’ll want a way to populate your indexing service on startup, so the SearchService can also be passes a list of objects for indexing: searchService.indexAll(Book.list()) or whatever.

Have fun tinkering with Groogle!