gpt4 book ai didi

ruby-on-rails - SearchKick中的日期范围查询( Elasticsearch )

转载 作者:行者123 更新时间:2023-12-02 23:28:49 24 4
gpt4 key购买 nike

我有一个列出事件集合的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

在我的postgres数据库中, start_datedate类型。

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

最佳答案

解决了。

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搜索结合使用。

关于ruby-on-rails - SearchKick中的日期范围查询( Elasticsearch ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com