Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searchkick index related model fields

I have a rails application and I'm switching from Sphinx to ElasticSearch and using the gem searchkick.

I have a model Teacher and a model Tags (via a gem), where a Teacher can have multiple tags associated. In the Teacher model I've defined the index like this:

def search_data
    {
      name: name,
      intro: intro,
      bio: bio,
      tag_name: tags.name
    }
end

Name, intro and bio are Teacher attributes, but I want to index the name od the tags associeted to the teacher. How can I do this?

The way it is now, it indexes the name of the object (relation), how can I index the attribute name inside the tag object?

like image 706
Adrian Matteo Avatar asked Jan 27 '14 14:01

Adrian Matteo


1 Answers

Shortly after asking the question, I found a solution on one of the issues on the github page:

def search_data
    {
      name: name,
      intro: intro,
      bio: bio,
      tag_name: tags.map(&:name)
    }
end

That indexes the correct attributes.

like image 138
Adrian Matteo Avatar answered Oct 21 '22 05:10

Adrian Matteo