本文介绍了Searchkick word_start与嵌套字段匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有searchkick gem的Rails应用程序.我的模型有一个嵌套的JSON字段.我尝试通过word_start匹配使其可搜索.当我显式设置为:
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'
如何使此嵌套的JSON字段与word_start匹配?
How can I make this nested JSON field to match word_start?
我在这里发现了相同的问题 https://github.com/ankane/searchkick/issues/1149 -但没有结果.
I found the same issue here https://github.com/ankane/searchkick/issues/1149 - but it has no result.
推荐答案
在这种情况下,您必须明确定义字段应如何在Elasticsearch中建立索引
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
这篇关于Searchkick word_start与嵌套字段匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!