Search code examples
ruby-on-railsruby-on-rails-3elasticsearchtire

Tire + Elasticsearch: How to search ranges of values?


Love Tire and elasticsearch so far but can't figure out how to search values between ranges like:

  • age_from age_to
  • 18 to 20

And return all Profile's where the age is between these 2 ranges? I have a integer column Profile.age

So far incomplete function

def self.search(params)

    #, default_operator: "AND
    tire.search(load: true, page: params[:page], per_page: 20) do
      query do
        should { string params[:query] } if params[:query].present?
        should { integer age_to[:age_to] } if params[:age_to].present?
      end
      to_curl
    end

  end

Solution

  • query do
        should { string params[:query] } if params[:query].present?
        should { range :age, { gte: params[:age_from], lte: params[:age_to] } } if params[:age_to].present? && params[:age_from].present?
    end