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.
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:
- Put a copy of lucene-core-2.0.0.jar and lucerne-highlight-2.0.0.jar into your project’s lib directory
- 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.
- Add the SearchService to your service classes
- Copy the supporting Groovy classes out of /src/groovy into your own project’s /src/groovy.
- Inject the SearchService into any controller that creates or updates your domain class
- 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.
- 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!
10 Comments + Add Comment
Leave a comment
Glen Smith
Archives
- April 2012
- March 2012
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





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.