2013-12-11 117 views
0

我把輪胎搜索在我的模型:ActiveRelation其中()輪胎搜索

class Name < ActiveRecord::Base 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 
    mapping do 
    indexes :name, type: 'string', analyzer: 'snowball' 
    indexes :lang, type: 'string' 
    indexes :private, type: 'boolean' 
    indexes :id, index: :not_analyzed, type: 'integer' 
    end 
end 

然後,當我執行:

txt = params[:search] 
Name.tire.search page: page, per_page: PER_PAGE do 
    string txt 
end 

如果效果很好,但我怎麼鏈更加搜索條件像:

Name.where(private: false, lang: ['ru', 'en'], id: [1,2,3,4]) 

我試圖做的:

@results = Name.tire.search per_page: per, page: page do 
    query do 
    boolean do 
     must { string txt } 
     must { term 'names.id', ids } unless ids.blank? 
     must { term 'names.private', false } 
     must { term 'names.lang', lang } 
    end 
    end 
end 

但它沒有返回任何結果..

回答

0

終於找到了解決辦法。

Name.tire.search per_pag: per, page: page do 
    query {string 'text'} 
    filter :term, private: false 
    filter :terms, lang: ['ru', 'en'] 
    filter :terms, id: [1,2,3,4] 
end 

注意,在「術語」和(期望數組)「條款」

1

嘗試:

Name.tire.search per_paga: per, page: page do 
    query {string txt} 
    filter :boolean, private: false 
    filter :array, lang: ['ru', 'en'] #here i'm not sure if is array or string 
end 
+0

感謝差異,部分它幫助我! –

相關問題