Saturday, April 12, 2008

Searchable: Me Too!

I've got to echo what seems to be the community consensus: use the Searchable plugin for Grails if you need to do any sort of searching or filtering. It just works and it works damn well. I'm using it to search/filter some lists of domain objects in my app with nice paging support. I added the plugin to an existing, deployed application in less than an hour this afternoon. The documentation was straightforward and easy to understand.

The only real trick I did was using the 'component' option for searching related domain classes. Take my domain class below:

class SampleRequest {
static searchable = true
static hasMany = [samples: Sample]
static mappedBy = [samples: "request"]
static belongsTo = User

// fields
User investigator
Hole hole
Double top
Double bottom
String sampleType
Integer samplesRequested = 1
Double sampleSpacing = 0.0
SampleGroup sampleGroup
String notes = ""
Date created = new Date()
String status = STATE_NEW
Integer priority = 1
}


By default, related domain classes such as User, Hole, and SampleGroup above are treated as references. When I was searching for something like "micropaleo" which happens to be the name of a SampleGroup object, Searchable would return the actual SampleGroup but not all of the SampleRequest objects in that group. Since I was mainly interested in the sample requests in that group, I simply changed my searchable definition to:

static searchable = {
hole component:true
investigator component:true
sampleGroup component:true
}

and added static def searchable = true to my User, Hole, and SampleGroup domain classes. So now when I search for something like "micropaleo" or "olney" the sample requests in that group or by that user are returned.

The best part is, my user's think I'm some sort of programming deity because they asked for search and I added it that same day. Hopefully none of them read this blog and see how little work it was for me.

Cheers.

4 comments:

Dave Klein said...

Don't worry your secret is safe with us!

Sen said...

Hi,
Thanks for this article As I am trying to make a search on Date Field and I gave the search as created::[2008-01-01 TO 2008-04-10] ] But when Iam trying that ,I get the following error: "org.compass.core.converter.ConversionException: Failed to parse date [2008-01-01]

Have you tried something with date field ???

Other than that searchable plugin is working perfectly fine...

Josh Reed said...

Hi Sen,

I haven't tried dates. I wonder if you can change the format in your SearchableConfiguration file:
Map defaultFormats = [(Date): "yyyy-MM-dd"]

The only thing I worry about is data in the database as it might not match this format.

Cheers,
Josh

Sen said...

Thanks a lot for your quick response.
I am sorry I am very much new to this plugin, so I guess there is a searchable configuration file in the plugin folder under searchable plugin, Do I have to go there and then format ? Do you know what's the file called .

Meanwhile I will dig into searchable plugin.

Thanks

Sen