使用hasMany的Grails可搜索插件

时间:2014-06-05 21:22:25

标签: grails plugins searchable

我正在使用grails searchable插件来搜索我的域类。但是,即使它们是简单类型String,我也无法通过我的hasMany(技能和兴趣)字段进行搜索。这是我的域类:

class EmpactUser {

static searchable = [except: ['dateCreated','password','enabled','accountExpired','accountLocked','passwordExpired']]

String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired

String email
String firstName
String lastName

String address
String phoneNumber
String description

byte[] avatar
byte[] resume

Date dateCreated

    static hasMany = [
        skills : String,
        interests : String, // each user has the ability to list many skills and interests so that they can be matched with a project.
]

static constraints = {
    username blank: false, unique: true
    password blank: false
    email email: true, blank: false

    firstName blank: false
    lastName blank: false

    description nullable: true
    address nullable: true
    avatar nullable: true, maxSize: 1024 * 1024 * 10
    resume nullable: true, maxSize: 1024 * 1024 * 10
    phoneNumber nullable: true, matches: "/[(][+]d{3}[)]d+/", maxSize: 30
}




}

这是我用来搜索的代码:

def empactUserList = EmpactUser.search(
            searchQuery,
            [reload: false, result: "every", defaultOperator: "or"])

我错过了什么吗?

谢谢, 艾伦。

1 个答案:

答案 0 :(得分:0)

Searchable无法识别与Strings的hasMany关系。解决方法是创建一个“属于”父对象的新域对象,并在父类中创建一个瞬态变量。以下代码用作the example given in documentation的替代代码。

class Article {
    static searchable = {
        root true
        keywords (component:true)
    }

    static transients = ['keywords']

    Set<ArticleKeyword> getKeywords() {
        ArticleKeyword.findAllByArticle(this) as Set
    }
}

class ArticleKeyword {
    static searchable = { root false}

    static constraints = {      
    }

    String text

    static belongsTo = [article:Article]
    static mapping = {
        text        type: 'text'
    }
}