可以在数组内搜索太阳黑子吗?

时间:2012-03-15 11:17:12

标签: ruby ruby-on-rails-3 solr sunspot

我有下一个带有数组字段的模型:

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

我在我的控制器中:

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

当我重新索引不显示错误但是:

这对我不适用。我在日志中得到了下一个错误:

Sunspot :: UnrecognizedFieldError(没有为名为'recipients'的邀请配置字段):

我也试过了:

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

但是当我重新索引时,我得到了下一个错误:

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

我可以解决此问题吗?

1 个答案:

答案 0 :(得分:7)

Invitation模型与has_manyrecipients关联。这意味着invitation可以有多个recipients

所以,试试这个:

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