ActiveRelation where() to tire search

92 views Asked by At

I put tire search in my model:

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

Then, when i perform:

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

If works well, but how do i chain more search conditions like:

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

I tried to do:

@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

But it not returning any results..

2

There are 2 answers

0
alexey_the_cat On BEST ANSWER

Finally found the solution.

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

Note the difference in "term" and "terms"(for array desired)

0
Aguardientico On

try with:

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