2010-10-14 52 views
3

我在我的Grails應用程序中使用了Searchable插件,但在返回有效的搜索結果時無法將它映射到兩個以上的域對象。我已經瀏覽了Searchable插件文檔,但找不到我的問題的答案。下面是我有域的一個非常基本的例子:如何在兩個以上的域對象上映射Grails可搜索插件?

class Article { 

    static hasMany = [tags: ArticleTag] 

    String title 
    String body 
} 

class ArticleTag { 
    Article article 
    Tag tag 
} 

class Tag { 
    String name 
} 

最終什麼,我希望做的是能夠通過搜索他們的標題,正文和相關標籤找到的文章。標題和標籤也會被提升。

映射這些類以滿足所需結果的正確方法是什麼?

回答

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(", ") 
    } 
} 
+0

這不正是我一直在尋找,但它確實工作。我正在使用你的解決方案,直到我找出更好的方法。感謝Aaron的幫助。 – aasukisuki 2010-10-19 15:18:51