Search code examples
searchgrailslucenegrails-plugincompass-lucene

How can I access child object property in Compas Search framework?


I have following code (in grails and Searchable Plugin aka Compass):

class Topic {

  String name;
  static searchable = true;
}

class Question extends BaseEntity {
  String question;

  static searchable = true;
  static hasMany = [
      topics: Topic
  ]
}

How can I search Question with specific topic id?

Something like Question.search("topics#id:12") or Question.search("topics.id:12") dosnt work.


Solution

  • Chage your searchable block in Question so it looks like this:

    static searchable = {
        topics component: true
    }
    

    and in Topic if you dont want Topics returned as root search elements

    static searchable = [
        root: false
    ]
    

    Fire up grails and add a few items, then download Luke from http://www.getopt.org/luke/ and open the index for your Question domain object which will be at ~/.grails/projects/projName/searchable-index/'env'/index/question

    If you check the documents tab you will see the terms embedded in the index which will be something like $/Question/topics

    This should give you the path to put in your Question.search, something like:

    Question.search('$/Question/topics/id:1')