简体   繁体   中英

Grails searchable plugin

In my Grails app, I have the following domain class that is indexed by the Searchable plugin:

class Foo implements Serializable {

    BookletCategory bookletCategory
    Date lastUpdated
    static hasMany = [details: BookletRootDetail]

    static searchable = {
        bookletCategory component: true
        id name: 'bookletRootId'
        lastUpdated index: 'no'
    }
}

When I retrieve instances of this class, the details property is always null, even though there are details instances in the database. My conclusion is that the details field is not being stored in the Lucene index. I tried adding this field to the index without making it a searchable property by adding the following to the searchable closure:

details index: 'no'

However it seems this is only valid for simple properties such as lastUpdated. Is there any way that I can ensure this property is populated when I retrieve instances of Foo from the index? Ideally I would like to do this without actually making details a searchable field?

Well, you can't retrieve an object that is not searchable itself, that's how it works, because you are not really touching the DB with the Searchable plugin. So you have two options:

  1. Make 'details' searchable (which obviously you don't want to).
  2. Refresh the objects. The problem is that you have to call refresh() on every single object of the list, and it implies a query to the DB, it's inefficient.

Right now, there are no other solutions to this problem.

I don't think you can do what you are trying to do. Assuming BookletRootDetail has a belongsTo clause (ie reference back to the parent), you could index the id, and make a second search query for the details corrsponding to the Foo's id.

You need to make sure that BookletRootDetail is searchable and then in Foo put it as a searchable component.

Something like:

class Foo implements Serializable {

    BookletCategory bookletCategory
    Date lastUpdated
    static hasMany = [details: BookletRootDetail]

    static searchable = {
        bookletCategory component: true
        id name: 'bookletRootId'
        lastUpdated index: 'no'
        details component: true
    }
}

Then

class BookletRootDetail {
  static searchable = true
}

You don't need BookletRootDetail to have a belongsTo relation with Foo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM