[英]How to set up a elasticsearch facet search on a nested rails model
是否可以在嵌套模型上设置构面搜索? 我有一个具有配置文件模型的用户模型。 我可以在用户模型中搜索配置文件模型中的术语。 现在我想通过配置文件模型中的location属性过滤搜索结果(用户)。
用户模型:
class User< ActiveRecord::Base
has_one :profile
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :profiles do
indexes :first_name
indexes :last_name
indexes :summary, type: 'string'
indexes :location, type: 'string', boost: 50
indexes :subjects, type: 'string', boost: 100
indexes :education, type: 'string'
end
end
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 10) do |s|
s.query { string params[:query]} if params[:query].present?
s.facet "locations" do
terms :location???
end
end
end
# Rest of class omitted
end
在搜索方法中,我不知道为“条款”放下什么。 terms :location???
视图:
<% @allusers.facets['locations'??]['terms'].each do |facet| %> **error on this line, locations cannot be nil**
<li>
<%= link_to_unless_current Profile.find(facet['term']).location, params.merge(id: facet['term']) %>
<% if params[:location??] == facet['term'].to_s %>
(<%= link_to "remove", location: nil %>)
<% else %>
(<%= facet['count'] %>)
<% end %>
</li>
<% end %>
在视图中我不知道用什么来代替[:location]
您应该能够使用terms "profiles.location"
替换terms :location
。 注意它现在是一个字符串而不是一个符号。 虽然您可能需要使用term
,或者您是否允许您的用户在其个人资料中拥有多个位置?
表单字段和构面名称无需更改。 你可以将你的方面命名为“foobar”,只要模型和视图中的方面名称相同,它仍然可以工作。 param是类似的,它只是保持值,可以命名任何东西。 我会考虑将它重命名为profile_location
只是为了遵循将下划线关联分开的rails约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.