gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:37:46 25 4
gpt4 key购买 nike

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

class Article {

static hasMany = [tags: ArticleTag]

String title
String body
}

class ArticleTag {
Article article
Tag tag
}

class Tag {
String name
}

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

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

最佳答案

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

关于plugins - 如何跨 2 个以上的域对象映射 Grails Searchable 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3937031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com