Activerecord 过滤关联';带弹性搜索和轮胎的ids
很长一段时间以来,我一直在为一个简单的问题伤脑筋。我在StackOverflow上查看了所有文档和示例,以及关于Activerecord 过滤关联';带弹性搜索和轮胎的ids,activerecord,elasticsearch,tire,Activerecord,elasticsearch,Tire,很长一段时间以来,我一直在为一个简单的问题伤脑筋。我在StackOverflow上查看了所有文档和示例,以及关于Tire的大多数问题,但没有成功 基本上,我正在尝试根据一些相关模型的ID过滤搜索结果 以下是模型(请注意,我目前仍然使用动态映射): 事实证明,动态映射并不能解决这种情况。我还必须定义数据的索引方式 以下是我的映射: mapping do indexes :id, index: :not_analyzed indexes :kind, index: :not_analyzed
Tire
的大多数问题,但没有成功
基本上,我正在尝试根据一些相关模型的ID过滤搜索结果
以下是模型(请注意,我目前仍然使用动态映射):
事实证明,动态映射并不能解决这种情况。我还必须定义数据的索引方式 以下是我的映射:
mapping do
indexes :id, index: :not_analyzed
indexes :kind, index: :not_analyzed
indexes :city_id, index: :not_analyzed
indexes :tags do
indexes :id, index: :not_analyzed
end
end
和我的自定义到_index_json
:
def to_indexed_json
{
kind: kind,
city_id: city_id,
tags: tags.map do |t|
{
id: t.id
}
end
}.to_json
end
最后,我可以这样过滤:
Location.search do
query { string params[:query] } if params[:query].present?
filter :term, { city_id: params[:city_id] } if params[:city_id].present?
filter :term, { "tags.id" => params[:tag_id] } if params[:tag_id].present?
filter :term, { kind: params[:kind] } if params[:kind].present?
end
重要的部分是标记索引,它允许我在过滤器中使用
“tags.id”
。这是正确的。顺便说一句,如果你没有使用分面导航,你应该使用过滤的
查询,而不是顶级的过滤器
@karmi有什么区别?此外,我可能最终会添加方面,这只是一项正在进行的工作。
def to_indexed_json
{
kind: kind,
city_id: city_id,
tags: tags.map do |t|
{
id: t.id
}
end
}.to_json
end
Location.search do
query { string params[:query] } if params[:query].present?
filter :term, { city_id: params[:city_id] } if params[:city_id].present?
filter :term, { "tags.id" => params[:tag_id] } if params[:tag_id].present?
filter :term, { kind: params[:kind] } if params[:kind].present?
end