vendredi 30 décembre 2016

Rails: Search by custom instance method's value using tire gem & elasticsearch

For example, I have Article model like

class Article < ActiveRecord::Base
  #Columns: id, title, status_number...etc

  STATUSES = {1 => "SUCCESS", 2 => "REJECTED"}

  include Tire::Model::Search
  include Tire::Model::Callbacks

  def display_status
    STATUSES[status_number]
  end

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 2) do
      query do
        boolean do
          must { string params[:query], default_operator: "AND" } if params[:query].present?
        end
    end
  end
end

how to include display_status as "SUCCESS" by default in search method? I tried

query do
  boolean do
    must { string params[:query], default_operator: "AND" } if params[:query].present?
    must { term :display_status , "SUCCESS" } 
  end
end

But couldn't get result. Please help to solve this problem. Thanks

Aucun commentaire:

Enregistrer un commentaire