【问题标题】:How do I map Grails Searchable plugin across more than 2 domain objects?如何将 Grails Searchable 插件映射到超过 2 个域对象?
【发布时间】:2010-10-14 20:00:03
【问题描述】:

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

class Article {

     static hasMany = [tags: ArticleTag]

     String title
     String body
}

class ArticleTag {
     Article article
     Tag tag
}

class Tag {
     String name
}

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

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

【问题讨论】:

    标签: plugins grails mapping searchable


    【解决方案1】:

    可能还有另一种方法,但这是我在应用程序中使用的简单方法。我向域对象添加了一个方法,以从标签中获取所有字符串值,并将它们与 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(", ")
        }
    }
    

    【讨论】:

    • 这不是我想要的,但它确实有效。我正在使用您的解决方案,直到找到更好的方法。感谢亚伦的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2014-09-06
    • 2012-07-09
    • 2011-09-14
    • 2016-03-11
    • 1970-01-01
    相关资源
    最近更新 更多