Ruby on Rails 中 Sunspot 的日期范围方面 [英] Date range facets with Sunspot in Ruby on Rails

查看:35
本文介绍了Ruby on Rails 中 Sunspot 的日期范围方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SunspotRuby on Rails 3 应用程序中搜索活动(派对、音乐会等).

I am using Sunspot to search for events (parties, concerts, ...) in a Ruby on Rails 3 application.

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

I have managed to set up free text search and facet search on a couple of different category types.

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

Now, I am stuck with my next task. I want to set up facets related to when the event is occuring.

我希望同时描述相对日期/时间范围,例如今天"、本周末"、下周末"和绝对日期/时间范围,例如2011 年复活节假期"、2012 年元旦"……日期时间范围有时会相互重叠.

I want to have facets describing both relative date/time ranges such as "today", "this weekend", "next weekend" and absolute date/time ranges, such as "Easter Holiday 2011", "New Years Day 2012", ... The datetime ranges are sometimes overlapping each other.

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

I have browsed around in the Sunspot API documentation, the Sunspot wiki, here at Stackoverflow, read and loads of articles and blogs. People are writing it is possible to to achieve but I find no examples or implementation ideas that makes me understand how to do this.

有什么建议吗?

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

Since my problem is not in my code, I don't publish any code. The class Event has a DateTime instance named "start_time". I do understand it's my job to define when the absolute date/time ranges appear in the calender.

最好的问候,

./stefan

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

PS Did I tell I'm a newbie? ;-) DS

推荐答案

为了高效的范围查询,您应该将字段设置为时间字段:

You should set up your fields as trie time fields for efficient range queries:

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

然后您可以使用查询分面来基于范围进行分面:

Then you can use query facets to facet based on ranges:

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

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

The key insight here is that you can construct facets using arbitrary scopes. Note that the facet name :start_time is just used to reference the facet in the search results, and the row labels :today and :tomorrow are similarly just used on the client side to identify the row counts corresponding to those queries; they have no meaning from Solr's standpoint, so you can call them whatever you want (using whatever data type you want -- they don't have to be symbols).

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

More information on query facets here: http://sunspot.github.com/docs/Sunspot/DSL/FieldQuery.html#facet-instance_method

这篇关于Ruby on Rails 中 Sunspot 的日期范围方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆