3

我在我的 Grails 应用程序中使用 Searchable 插件,但在返回有效搜索结果时无法让它映射超过 2 个域对象。我查看了可搜索插件文档,但找不到我的问题的答案。这是我拥有的域的一个非常基本的示例:

class Article {

     static hasMany = [tags: ArticleTag]

     String title
     String body
}

class ArticleTag {
     Article article
     Tag tag
}

class Tag {
     String name
}

最终,我想要做的是能够通过搜索文章的标题、正文和相关标签来找到文章。标题和标签也将得到提升。

映射这些类以满足所需结果的正确方法是什么?

4

1 回答 1

3

可能还有另一种方法,但这是我在应用程序中使用的简单方法。我向域对象添加了一个方法,以从标签中获取所有字符串值,并将它们与 Article 对象一起添加到索引中。

这使我可以只搜索文章域对象并获得我需要的一切

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(", ")
    }
}
于 2010-10-14T21:45:17.403 回答