Last updated by brunogh 4 years ago

Class Property Mapping

If you want to override the conventional mapping for one or more properties, make the value of searchable a Closure and add method calls named after the class property:

class Post {
    static searchable = {
        category index: 'not_analyzed', excludeFromAll: true
        title boost: 2.0
        comments component: true
    }
    static hasMany = [comments: Comment]
    User author
    String title, post, category
    Date createdAt
}

The mapping options available depend on the kind of class property.

Mapping the id

Compass needs to know the object's identifier property, so that needs to be mapped too.

Since Grails has the well defined and generated id property, the plugin generates an id mapping.

You can customize this with an explicit id mapping.

Mapping simple types

For simple types, you can declare any of the searchable property mapping options.

Example

Given the following class:

class Article {
    static searchable = true
    static hasMany = [keywords: String]
    String title, body
}

we currently have a conventional mapping because it declares simply static searchable = true@. This maps both @title and body as standard searchable properties.

Let's define a few additional searchable property mapping options to customize the conventional mapping to our needs:

class Article {
    static searchable = {
        title boost: 2.0
        keywords index: 'not_analyzed'
    }
    static hasMany = [keywords: String]
    String title, body
}

In this case we've given title a boost@, which means hits in the @title property score higher than other properties, and we've declared keywords as @not_analyzed@, meaning it's value is stored verbatim in the index.

Mapping associated domain classes

For associated domain classes you have two choices:

  1. You can inherit and add to the conventional mapping - whether a searchable reference or searchable component - and add to it with the supported searchable reference mapping options or searchable component mapping options.
  2. Or you can override the conventional mapping - of searchable reference or searchable component to be the other.

Example

Given the following classes:

class News {
    static searchable = true
    static hasMany = [comments: Comment]
    String text
}

class Comment {
    static searchable = true
    String text
    News news
}

We start out with the conventional mapping, since Comment and News declare @static searchable = true@.

This maps the classes by convention meaning:

Inherit and add

To inherit the Comment#news searchable reference mapping and add searchable reference mapping options:

class Comment {
    static searchable = {
        news cascade: ['create', 'delete']
    }
    String text
    News news
}

If you like you can also make the conventional mapping explicit, ie, declare it as a searchable reference or searchable component:

class Comment {
    static searchable = {
        news reference: [cascade: ['create', 'delete']]
    }
    String text
    News news
}

The value of reference or component may be true or a Map of options.

Override

To override the News#comments searchable reference mapping and make it a searchable component instead:

class News {
    static searchable = {
        comments component: true
    }
    static hasMany = [comments: Comment]
    String text
}

You can also specify searchable reference mapping options or searchable component mapping options options with a Map value:

class News {
    static searchable = {
        comments component: [cascade: 'all', accessor: 'field']
    }
    static hasMany = [comments: Comment]
    String text
}