How do I map Grails Searchable plugin across more than 2 domain objects?
I'm using the Searc开发者_如何学编程hable 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?
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(", ")
}
}
精彩评论