Last updated by brunogh
3 years ago
Class Property Mapping
If you want to override the conventional mapping for one or more properties, make the value ofsearchable 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
}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 generatedid 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
}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
}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:- 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.
- 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
}Comment and News declare @static searchable = true@.This maps the classes by convention meaning:
- @News#comments@ becomes a searchable reference
- @Comment#news@ becomes another searchable reference
Inherit and add
To inherit theComment#news searchable reference mapping and add searchable reference mapping options:class Comment {
static searchable = {
news cascade: ['create', 'delete']
}
String text
News news
}class Comment {
static searchable = {
news reference: [cascade: ['create', 'delete']]
}
String text
News news
}reference or component may be true or a Map of options.Override
To override theNews#comments searchable reference mapping and make it a searchable component instead:class News {
static searchable = {
comments component: true
}
static hasMany = [comments: Comment]
String text
}Map value:class News {
static searchable = {
comments component: [cascade: 'all', accessor: 'field']
}
static hasMany = [comments: Comment]
String text
}