4

我正在使用Sunspot在Ruby on Rails 3 应用程序中搜索事件(派对、音乐会等) 。

我设法在几个不同的类别类型上设置了自由文本搜索和方面搜索。

现在,我被我的下一个任务困住了。我想设置与事件发生时间相关的方面。

我想要描述相对日期/时间范围(例如“今天”、“本周末”、“下周末”)和绝对日期/时间范围(例如“2011 年复活节假期”、“2012 年元旦”)的方面。 .. 日期时间范围有时会相互重叠。

我在 Stackoverflow 上的 Sunspot API 文档、Sunspot wiki 中浏览过,阅读并阅读了大量文章和博客。人们正在写它是可能实现的,但我没有找到让我理解如何做到这一点的示例或实现想法。

有什么建议么?

由于我的问题不在我的代码中,因此我不发布任何代码。Event 类有一个名为“start_time”的 DateTime 实例。我明白我的工作是定义绝对日期/时间范围何时出现在日历中。

最好的祝福,

./斯蒂芬

PS我说我是新手吗?;-) DS

4

2 回答 2

19

您应该将字段设置为尝试时间字段以进行有效的范围查询:

class Event
  searchable do
    time :start_time, :trie => true
  end
end

然后,您可以使用查询构面来根据范围进行构面:

Event.search do
  facet :start_time do
    bod = Time.zone.now.beginning_of_day
    row :today do
      with :start_time, bod..(bod + 1)
    end
    row :tomorrow do
      with :start_time, (bod + 1)..(bod + 2)
    end
    # etc.
  end
end

这里的关键见解是您可以使用任意范围构建构面。请注意,构面名称:start_time仅用于引用搜索结果中的构面,而行标签:today:tomorrow类似地仅在客户端用于标识与这些查询对应的行数;从 Solr 的角度来看,它们没有任何意义,因此您可以随意调用它们(使用您想要的任何数据类型——它们不必是符号)。

有关查询方面的更多信息:http: //sunspot.github.com/docs/Sunspot/DSL/FieldQuery.html#facet-instance_method

于 2011-02-24T14:11:57.633 回答
0

你可以这样做:

with(:registration_date).between(params[:start_date].to_date..params[:end_date].to_date)

看这里:http ://sunspot.github.io/docs/Sunspot.html#search-class_method

于 2013-12-11T20:27:18.923 回答