Search code examples
ruby-on-railselasticsearchsearchkick

Searchkick word_start match with nested field


I have a Rails app with searchkick gem. My model has a nested JSON field. I try to make it searchable with word_start match. When I set in explicitly like:

class Post < ApplicationRecord 
  searchkick word_start: [:nested_data_field]
end

I does not work, and I get error:

{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse [nested_data_field]", "caused_by"=>{"type"=>"illegal_state_exception", "reason"=>"Can't get text on a START_OBJECT at 1:401"}} on item with id '2596'

How can I make this nested JSON field to match word_start?

I found the same issue here https://github.com/ankane/searchkick/issues/1149 - but it has no result.


Solution

  • in this case, you have to explicitly define how fields should index in elasticsearch

    class Post < ApplicationRecord 
      searchkick word_start: [:author_name, author_country]
    
      def search_data # override indexing fields
        {
         tite: self.title,
         date: self.created_at,
         author_name: author[:name], # nested json(author field)
         author_country: author[:country],
         comments_ids: comments.map(&:id) # index as an array of ids       
        }
      end
    end