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

Question

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

Answers 0
Added at 2016-12-30 16:12
Tags
Question

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

Answers
Source Show
◀ Wstecz