我有一个带有searchkick gem的Rails应用程序。我的模型有一个嵌套的JSON字段。我尝试通过word_start匹配使其可搜索。当我明确设置为:
class Post < ApplicationRecord
searchkick word_start: [:nested_data_field]
end
我无法工作,并且出现错误:
{"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'
如何使此嵌套JSON字段匹配word_start?
我在https://github.com/ankane/searchkick/issues/1149中发现了相同的问题-但没有结果。
最佳答案
在这种情况下,您必须明确定义字段在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
关于ruby-on-rails - Searchkick word_start与嵌套字段匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55430115/