[英]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.