I'm using the chewy gem for elasticSearch with Rails. I have related documents, and I want to show both the fields with highlighting and some other fields in my results.
When I get search results back the highlighting shows up for matches in the "main" record but not in the "related records" (not sure what the correct terminology is).
My search_index.rb looks like this:
define_type PlayList.includes(:play_list_items,:user) do
field :title
field :description
field :thumb_missing, value: ->{intro_image_file_name.nil?}
field :thumb_square, value: ->{intro_image(:thumb_square)}
field :play_list_items do
field :item_title, value: ->{title}
field :commentary_text
field :thumb_square, value: ->{get_image(:thumb_square)}
end
field :user do
field :name
field :avatar, value: ->{avatar(:tiny)}
end
end
and the query looks like this
@play_lists = SearchIndex::PlayList.query(multi_match: {
query: params[:term],
fields: ["title","description","item_title","commentary_text"]
}).highlight(
pre_tags:["<mark>"],
post_tags:["</mark>"],
fields: {
title:{},
description:{},
item_title:{},
commentary_text:{}
})
When I get a match on the title, the title method returns the title text with the term highlighted. However, I always get the play_List_items back as an array of hashes with no highlighting.
When I serialize using "to_a" I see that in the _data there is a hash with a field called "highlight" which includes the highlighting, but it only had the fields that match, so I wouldn't know how to display the associated thumb_square (which appears in _data.source) together with the item_title with highlighting.
I don't know if this is related to chewy gem or to elasticsearch in general.
This is my non elegant solution. Sorry I didn't clean up any extrenious code.