3
votes

I'm using the Searchable plugin in my Grails application, but am having trouble getting it to map across more than 2 domain objects while returning valid search results. I've looked through the Searchable plugin documentation, but cannot find the answer to my question. Here's a very basic example of the domains I have:

class Article {

     static hasMany = [tags: ArticleTag]

     String title
     String body
}

class ArticleTag {
     Article article
     Tag tag
}

class Tag {
     String name
}

Ultimately what I'm looking to do is be able to find articles by searching their titles, body and associated tags. The titles and tags would be boosted as well.

What's the proper way to map these classes to meet the desired results?

1

1 Answers

3
votes

There is probably another approach, but this is the simple approach I used in my application. I added a method to the domain object to get all of string values from the tags and add them to the index with the Article object.

This allows me to just search the Article domain object and get everything I need

class Article {

    static searchable = { 
        // don't add id and version to index
        except = ['id', 'version']

        title boost: 2.0
        tag boost:2.0

        // make the name in the index be tag
        tagValues name: 'tag'
    }

     static hasMany = [tags: ArticleTag]


     String title
     String body

    // do not store tagValues in database
    static transients = ['tagValues']

    // create a string value holding all of the tags
    // this will store them with the Article object in the index
    String getTagValues() {
        tags.collect {it.tag}.join(", ")
    }
}