Search code examples
pluginsgrailssearchable

Grails searchable: search for a specific field in a member object?


Using the Grails Searchable plugin, I've got these classes:

class Person {
 static searchable = {
  address component: true
    }
}

and:

class Address {
 static searchable = {
  root false
 }
 String country
}

I want to do a specific search for persons from a specific country. "country:NL" doesn't work. "address:country:NL" doesn't work either. I can't find anything about the syntax for this. Any ideas?

I think I'll have to do some clever indexing or some other trick in the searchable closure, but I just can't find it.


Solution

  • I created a basic app (Grails 1.3.5, Searchable 0.5.5.1) with your two classes and searching for 'country:NL' works for me. Did you remember to call index() before trying to search?

    grails create-app search
    grains install-plugin searchable
    

    Person:

    class Person {
      static searchable = {
        address component: true
      }
    
      Address address
    }
    

    Address:

    class Address {
      static belongsTo = Person
      static searchable = {
        root false
      }
      String country
    }
    

    Bootstrap:

    class BootStrap {
    
        def init = { servletContext ->
    
          def p1 = new Person(address:new Address(country:'NL')).save()
          def p2 = new Person(address:new Address(country:'DE')).save()
          def p3 = new Person(address:new Address(country:'NZ')).save()
    
          Person.index()
    
        }
    
        def destroy = {
        }
    }
    

    Then I browsed to to /searchable and searched for country:NL and got person 1 returned.

    If you want to see what Searchable is doing under the covers with regards to fields/indexes etc - Luke is a very handy tool (just download the executable JAR): http://code.google.com/p/luke/

    The index files are in

    <user.home>/.grails/projects/<project name>/searchable-index/development/index
    

    cheers

    Lee