ActiveRelation where()轮胎搜索

时间:2013-12-11 12:22:26

标签: ruby-on-rails search tire

我把轮胎搜索放在我的模型中:

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

但它没有返回任何结果..

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

请注意“术语”和“术语”(对于所需数组)的区别