Jan
9
2007

Groogle: Adding a Simple Search facility to Grails

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
  • SearchController which takes the user queries, sends them off to the search service, and then sends the results off to the view
  • search.gsp to put it all on the web
  • Some supporting Groovy classes to act as holders for the search results, hightlighted hits, and whatnot.

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!

About the Author: Glen Smith

10 Comments + Add Comment

  • Hi Glen, Very interesting and am keen to see if you had any progress with the plugin? The compass plugin looks good, but it won’t work with 0.4.2. Anyway – please keep me posted, you have one interested party here!!!

  • Sadly the progress has been slow here.

    It should be very straightforward to integrate the existing SearchService with your code, though. Drop me a line if you’re keen and I’ll talk you through it.

  • hey this great…
    nice work! i just implemeted it, and it s very clear and working fine!

    greetz rainer

  • Great stuff, mate! Glad it’s working out for you.

  • Hello all!

    I get

    Server failed to start: org.mortbay.util.MultiException[org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.Gr
    oovyRuntimeException: Cannot add new method [getLog] for arguments [[]]. It already exists!, org.codehaus.groovy.runtime.Invok
    erInvocationException: groovy.lang.GroovyRuntimeException: Cannot add new method [getLog] for arguments [[]]. It already exist
    s!, org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.GroovyRuntimeException: Cannot add new method [getLog]ade
    for arguments [[]]. It already exists!]

    after grails upgrade. I run grails 0.5.6.

    Thanks,
    Christoph

  • Hi Chris. You’ll need to remove the “def log = LogFactory…” lines at the top of each file.

    Newer versions of Grails now inject a log object into everything automagically.

  • (Thank you Glen!)

    Has anyone tried the Searchable Plugin?

    http://grails.codehaus.org/Searchable+Plugin

    What are the main differences (because it is also based on Lucene)?

  • hi, i deployed my app using this search on tomcat and under windows the “lucene-index” directory lands under “tomcat/bin/*” and under unix the “lucene-index” lands under “tomcat/*”…

    how can i make the directory end up under “tomcat/webapps/myApp/*” ???

    thx a lot… i like what u ve done ;)

    rainer

  • Hey Glen, I was looking for Groovy and Lucene integration and hit upon your article. Could you please help me out or point me to the right resource. Thanks.

  • Hi Naveen. You can download the “groogle” source from the link above. There’s plenty in there to get you started.

Leave a comment

Glen Smith

About Glen

Co-author Grails in Action