3

I'm using Tire gem with eleasticsearch and I want to filter results of the query like a Rails scope do. I want to exclude different parameters of the result like I have done with this scope :

scope :online, ->{ where.not(price: nil || 0).where.not(name: 'Undefined').where(online: true) }

How can I do this with my self.search method? Is my method correct?

Here is my Product model :

Class Product
  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :name, type: 'string', analyzer: 'snowball', boost: 100
    indexes :description, analyzer: 'snowball'
    indexes :price, type: 'float'
    indexes :category, type: 'string'
    indexes :location, type: 'string'
    indexes :online, type: 'boolean'

    indexes :tags do
      indexes :name, type: 'string', analyzer: 'snowball', boost: 100
    end
  end

  def to_indexed_json
    {
      name: name,
      description: description,
      price: price,
      category: category,
      location: location,
      online: online,
      created_at: created_at,
      updated_at: updated_at,
      tags: tags
    }.to_json
  end

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 12, query: {"match_all" => {}} ) do
      query { string params[:query], default_operator: "AND" } 
    end
  end
end
Jonnyx Delavilla
  • 555
  • 9
  • 24

0 Answers0