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 オブジェクトを使用してインデックスに追加しました。

これにより、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 に答える