SearchKick中的日期范围查询(弹性搜索)

时间:2016-08-23 11:08:08

标签: ruby-on-rails ruby elasticsearch

我有一个Rails 5应用程序,列出了一系列事件。我已经很好地设置了SearchKick,我现在正在尝试添加仅显示今天事件的功能,但我正在努力实现。我目前有以下内容:

events_controller.rb

class EventsController < ApplicationController

    def index
      query = params[:j].presence || "*"
      conditions = {}
      conditions[:sector_name] = params[:sector_name] if params[:sector_name].present?
      conditions[:service_name] = params[:service_name] if params[:service_name].present?
      conditions[:start_date] = params[:start_date] if params[:start_date].present?
      @events = Event.search query, where: conditions, aggs: [:sector_name, :service_name], range: {start_date: {"gte": "now/d"}}, order: {start_date: {order: "asc", unmapped_type: "long"}}, page: params[:page], per_page: 10
    end
  ...
end

start_date在我的postgres db中属于date类型。

当我转到index.html.erb时,我仍然在今天的日期之前看到start_date的事件。

1 个答案:

答案 0 :(得分:1)

完成了工作。

def index
  query = params[:j].presence || "*"
  conditions = {start_date: {"gte": "now/d"}}
  conditions[:sector_name] = params[:sector_name] if params[:sector_name].present?
  conditions[:service_name] = params[:service_name] if params[:service_name].present?
  conditions[:start_date] = params[:start_date] if params[:start_date].present?
  @events = Event.search query, where: conditions, aggs: [:sector_name, :service_name], order: {start_date: {order: "asc", unmapped_type: "long"}}, page: params[:page], per_page: 10
end

我只是将start_date: {"gte": "now/d"}传递给条件哈希。这意味着它现在可以与我的aggs搜索结合使用。