MeIr
MeIr

Reputation: 7316

Grails searchable returns domain but relation is null

I have 3 domains: A, B, C.

A and C have many-to-many relationship through B. A and C are searchable.

When I search and get list of A domain all the fields in A are accessible, however relation field is always 'null'. Why I can't access relational field? Why do I get 'null'? If I access object directly, I see a relation, but when searchable returns A domain, I get 'null' on a relation field.

P.S: I tried to make B searchable but it looks like searchable having issue with indexing it on top of that I don't see any point in indexing B since it exists for the sake of many-to-many relationship only.

Please I need help with it. I need to be able to get to C via A on a searchable return.

Thank you.

[Update]: I made a prototype project today (small DB) and made domain B searchable. Now I can access relational field. However in my real project, I don't want to make B searchable since database is big and indexing takes too long. Is there a way around it?

Upvotes: 1

Views: 362

Answers (2)

demon101
demon101

Reputation: 564

You can refrash all instance in result list or use reload:true property for seach method

searchableService.search(query,[reload:true]) 

You can find full list of options: http://grails.org/Searchable+Plugin+-+Methods+-+search

reload - If true, reloads the objects from the database, attaching them to a Hibernate session, otherwise the objects are reconstructed from the index. Default is false

Upvotes: 2

MeIr
MeIr

Reputation: 7316

Ok, I believe I solved my issue. First checkout link to a similar question: Grails searchable plugin -- Please give GalmWing some credit.

My solution is following, I am implementing my own controller and following piece of code implements searchable service call:

    if (params.q) {
        try{
            def searchResults = searchableService.search(params.q, params)


            searchResults.results.each {
                it.refresh()
            }

            return [carInstanceList:searchResults.results, carInstanceTotal:searchResults.total]
        } catch (SearchEngineQueryParseException ex) {
            return [parseException: true]
        }

As you can see I have a loop where on each item of domain "A" I do refresh. Now refresh gets real record from DB with all the links. Now I return list into GSP and it extracts all of "C" domains associated with the "A" domain.

Now this way you might get performance penalty, but in my case searchable is actually not able to index "B" domain, it is working for a while and then crashes, so I don't have another choice, at least for now.

Upvotes: 0

Related Questions