Rails Searchkick / Elasticsearch has_many and belongs_to associations Rails Searchkick / Elasticsearch has_many and belongs_to associations elasticsearch elasticsearch

Rails Searchkick / Elasticsearch has_many and belongs_to associations


In your Book model you need to have a search_data block for the indexing.

def search_data  attributes.merge(    author_name: author(&:name)    publisher_name: publisher(&:name)    subjects_name: subjects.map(&:name)  )end

this will add the associations to your index.

You use the .map method for the has_many associations.