是否可以在嵌套模型上设置构面搜索?我有一个具有配置文件模型的用户模型。我可以在用户模型中搜索个人文件模型中的术语。现在,我想通过概要文件模型中的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???
View :
<% @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 %>
在 View 中,我不知道该用什么代替
[:location]
最佳答案
您应该可以将terms :location
替换为terms "profiles.location"
。请注意,它现在是字符串而不是符号。尽管您可能需要改用term
,还是允许用户在其个人资料中拥有多个位置?
表单字段和构面名称无需更改。您可以将构面命名为“foobar”,只要构面名称在模型和 View 中相同,它就仍然可以使用。参数是相似的,它只是保存值,可以命名为任何东西。我会考虑将其重命名为profile_location
只是为了遵循用下划线分隔关联的rails约定。
关于ruby-on-rails - 如何在嵌套的Rails模型上设置Elasticsearch方面搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20047492/