這裏是我最初的搜索塊看起來像我的用戶模型:如何使用Sunspot/Solr在has_many關聯中進行搜索?
searchable do
text :name
integer :sport_ids, multiple: true do
sports.map(&:id)
end
integer :position_ids, multiple: true do
positions.map(&:id)
end
integer :city_id
integer :school_id
string :state
end
如何通過has_many關聯搜索?我需要它返回每個在運動或sport_positions中都有指定ID的運動員。所以如果有人從下拉菜單中選擇「籃球」,身份證號碼2會傳遞給我的搜索方法,並且需要返回sport_id爲2的運動員。下面是如何運動和sport_positions在用戶模式聲明:
has_and_belongs_to_many :positions, :class_name => "SportPosition", :join_table => "user_sport_positions", :uniq => true
has_many :sports, :through => :user_sports, order: "user_sports.created_at", class_name: "Sport"
has_many :user_sports
:::編輯:::
這工作一分鐘在我重新編制索引之後,突然間我開始出現這個錯誤:
Sunspot::UnrecognizedFieldError (No field configured for Athlete with name 'sport_ids'):
app/models/search.rb:12:in `block in execute'
這裏是我的搜索模式:
class Search < ActiveRecord::Base
attr_accessible :coach_id, :sport_id, :gpa_min, :gpa_max, :sport_position_id,
:classification, :city_id, :state, :school_id, :athlete_name
belongs_to :coach
def self.execute(params, page = nil)
Sunspot.search(Athlete) do |query|
query.with :public, true
query.with :removed_from_listing, false
query.fulltext params[:athlete_name] unless params[:athlete_name].blank?
query.with :sport_ids, params[:sport_id] unless params[:sport_id].blank?
query.with :position_ids, params[:sport_position_id] unless params[:sport_position_id].blank?
query.with(:state).equal_to(params[:state]) unless params[:state].blank?
query.with(:classification).equal_to(params[:classification]) unless params[:classification].blank?
query.with :city_id, params[:city_id] unless params[:city_id].blank?
query.with :school_id, params[:school_id] unless params[:school_id].blank?
query.with(:current_gpa).between(params[:gpa_min]..params[:gpa_max]) unless params[:gpa_min].eql?("0.0") && params[:gpa_max].eql?("5.0")
query.paginate page: page unless page.blank?
end
end
end
注:爲了使這個更奇怪的,我已經叫場「recruit_year」這是一個整數屬性。我在這個領域得到同樣的錯誤,說「沒有配置字段」等等等等。如果您嘗試像equal_to那樣進行比較或將其視爲string
,則通常只會在text
字段發生該錯誤。
幫助?