Can sunspot search inside array?

不羁岁月 提交于 2019-12-21 05:04:32

问题


I have the next model with a array field:

Class Invitation
 include Mongoid::Document
 include Mongoid::Timestamps::Created
 include Sunspot::Mongo

 field :recipients, :type => Array
 attr_accessible :recipients

 searchable do
  text :recipients do
  recipients.map { |recipient| recipient }
  end
 end

end

I have in my controller:

def recipients

 @invitation = Invitation.find(params[:id])
 @search = Invitation.search do |s|
 s.fulltext params[:search]
 s.with(:recipients, @invitation.recipients)
 end

@recipients = @search.results
  respond_to do |format|
   format.html
  end

end

This when I reindex not show error but:

This not works fine for me. I get the next error in log:

Sunspot::UnrecognizedFieldError (No field configured for Invitation with name 'recipients'):

I have tried too:

string :recipients do
  recipients.map { |recipient| recipient }
end

But I get the next error when I reindex:

recipients is not a multiple-value field, so it cannot index values []

Can I do fix this problem?


回答1:


The Invitation model has a has_many association with recipients. This means an invitation can have multiple recipients.

So, try this:

string :recipients, :multiple => true do
  recipients.map { |recipient| recipient }
end


来源:https://stackoverflow.com/questions/9718584/can-sunspot-search-inside-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!