如何跨 2 个以上域对象映射 Grails Searchable 插件?

发布于 2024-09-27 16:07:46 字数 404 浏览 7 评论 0原文

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

class Article {

     static hasMany = [tags: ArticleTag]

     String title
     String body
}

class ArticleTag {
     Article article
     Tag tag
}

class Tag {
     String name
}

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

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

I'm using the Searchable 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2024-10-04 16:07:46

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

这允许我只搜索 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(", ")
    }
}

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(", ")
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文