Searchkick word_start match with nested field

∥☆過路亽.° 提交于 2021-01-29 09:05:49

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/55430115/searchkick-word-start-match-with-nested-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!