0

我有2個atrributes模型:配置型號名爲「IMAGE_FILENAME」無場

:image_filename 
:yt_video_id 

我有這樣的代碼在我的控制器:

def index 
    @search = Model.solr_search do |s| 
    s.fulltext params[:search] 
    s.paginate :page => params[:page], :per_page => 2 
    s.with(:image_filename || :yt_video_id) 
    end 
    @model = @search.results 
    respond_to do |format| 
    format.html # index.html.erb 
    end 
end 
model.rb型號

我有這個在searchable

searchable do 
    string :image_filename, :yt_video_id 
    end 

我想篩選:image_filename:yt_video_id任何不是"nil"。我的意思是,這兩個屬性都必須有一個強制值。

,但我得到的錯誤:

Sunspot::UnrecognizedFieldError in ModelsController#index 

No field configured for Model with name 'image_filename' 

回答

2

的問題得到了解決下面的步驟:

(。這個解決方案工作正常,我希望這個解決方案可以幫助你太)

model.rb,你可以不寫這句法:

searchable do 
    string :image_filename, :yt_video_id 
    end 
在索引操作

searchable do 
     string :image_filename 
     string :yt_video_id 
    end 

在你models_controller.rb

你必須寫這句法

def index 
    @search = Model.solr_search do |s| 
    s.fulltext params[:search] 
    s.paginate :page => params[:page], :per_page => 2 
    s.any_of do 
     without(:image_filename, nil) 
     without(:yt_video_id, nil) 
    end 
    end 
    @model = @search.results 
    respond_to do |format| 
    format.html # index.html.erb 
    end 
end 

我已經使用了any_of方法。

要使用OR語義合併範圍,使用any_of方法組限制到一個脫節:

Sunspot.search(Post) do 
    any_of do 
    with(:expired_at).greater_than(Time.now) 
    with(:expired_at, nil) 
    end 
end 

可以在https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields

+1

看能否請你解釋一下,爲什麼我們不能像寫這個'string:image_filename,:yt_video_id'? [搜索與黑子](http://railscasts.com/episodes/278-search-with-sunspot) 在視頻中,他們使用了這種語法,但對我來說,這是行不通的。 – Chezhian