Can't get highlighting on search results for relations using chewy gem

934 views Asked by At

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.

1

There are 1 answers

1
tomf On

This is my non elegant solution. Sorry I didn't clean up any extrenious code.

class SearchController < ApplicationController

  def index
  play_lists =  SearchIndex::PlayList.query(multi_match: {
    query: params[:term], 
    fields: ["title","description","item_title","commentary_text","name","user_id"]
    }).filter(term:{public:true}).highlight(
        pre_tags:["<mark>"],
        post_tags:["</mark>"],
        fields: {
          title:{},
          description:{},
          item_title:{},
          commentary_text:{},
          name:{}
          }
        ).to_a

     # search results include mark for title and description but not for item_title or commentary_text
     # if any item leve matches, find associated items and show with highlights from _data  
     @play_lists=play_lists.map do |play_list|
       match_indexes=Set.new
       highlight= play_list._data["highlight"]
       if (highlight["item_title"])
         highlight["item_title"].each do |hl_title|
           nhl_title=hl_title.gsub("<mark>","").gsub("</mark>","")  #remove the highlighting so we can do comparisons.
           play_list.play_list_items.each_with_index do |pli, ndx|
             if (pli["item_title"]==nhl_title)  # if match without highlight copy in highlight
               pli["item_title"]=hl_title
               match_indexes.add(ndx)
             elsif pli["item_title"].to_s.include? nhl_title
               pli["item_title"]="..."+hl_title+"..."
               match_indexes.add(ndx)
             end
           end
         end
       end
       if (highlight["commentary_text"])
         highlight["commentary_text"].each do |hl_ct|
           nhl_ct=hl_ct.gsub("<mark>","").gsub("</mark>","")  #remove the highlighting so we can do comparisons.
           play_list.play_list_items.each_with_index do |pli, ndx|
             if pli["commentary_text"]==nhl_ct  # if match without highlight copy in highlight
               pli["commentary_text"]=hl_ct
               match_indexes.add(ndx)
            elsif pli["commentary_text"].to_s.include? nhl_ct
              pli["commentary_text"]="..."+hl_ct+"..."
              match_indexes.add(ndx)
             end
           end
         end
       end
       if match_indexes.empty?
         items_to_show=play_list.play_list_items.slice(0,3)  # we will only show fist 3 if no matches 
       else
         items_to_show=[]
         match_indexes.each do |mi|
           items_to_show.push (play_list.play_list_items[mi])
         end
       end
       pl=play_list.attributes
       pl["play_list_items"]=items_to_show
       pl["user"]["name"] = highlight["name"][0] if (highlight["name"])
       if pl["title"].is_a? Array
         pl["title"]="..."+pl["title"].join("...")+"..."
       end
       pl["description"] ||=""
       if pl["description"].is_a? Array
         pl["description"]="..."+pl["description"].join("...")+"..."
       end
       pl           
     end
end