【问题标题】:Chewy Gem - Query to get ranges of dates containing specific dateChewy Gem - 查询以获取包含特定日期的日期范围
【发布时间】:2015-09-17 22:41:57
【问题描述】:

我在 Rails 应用程序上使用 ElasticSearch 和耐嚼的 GEM。我会帮忙翻译一下:

all.select { |task|
 (task.start_at.to_date..task.end_at.to_date).cover?(Date.today)
}

使用耐嚼的过滤器。我尝试长时间完成这项工作,但没有成功。

任何形式的帮助都会很棒。

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch chewy-gem


    【解决方案1】:

    我知道这已经很久了,但我现在正在使用 Chewy gem,所以就这样吧:

    假设您有一个TasksIndex,其中start_atend_at 字段定义为date 类型并且具有正确的format

    field :start_at, type: 'date', format: 'basic_date_time_no_millis' # example, you can use other formats
    field :end_at, type: 'date', format: 'basic_date_time_no_millis'
    

    那么你的过滤器可以是这样的:

    def tasks_including_date(target_date)
      formatted_target_date = target_date.strftime('%Y-%m-%d')
      TasksIndex.filter{start_at <= formatted_target_date}.filter{end_at >= formatted_target_date}
    end
    

    那么你可以:

    tasks_including_date(Date.today)
    

    如果您想要结果本身,而不仅仅是过滤范围,只需在结果上使用to_ary。它会处理你的结果。

    【讨论】:

      【解决方案2】:

      此代码遍历集合“all”。对于每个项目,如果今天在任务的执行时间范围内(它今天的日期在 begin_at 和 end_at 范围之间),即是否应该执行任务,它会检查并返回 true。

      【讨论】:

      • 是的,这就是代码的作用。我会把它翻译成一个弹性搜索查询。
      • UsersIndex.filter{ ~(start_at.to_date = Date.today) }
      猜你喜欢
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多