按日期范围过滤 Sphinx 搜索结果

发布于 2024-07-09 10:05:34 字数 386 浏览 4 评论 0原文

我有 Widget.title、Widget.publish_ at 和 Widget.unpublish_ at。 这是一个 Rails 应用程序,正在运行thinking_sphinx,每晚索引一次。 我想找到标题中包含“foo”并已发布的所有小部件(publish _at < Time.now,unpublish _at > Time.now)。

为了使分页正常工作,我真的想在 sphinx 查询中执行此操作。 我有 'has :publish_at, :unpublish_at' 来获取属性,但是 'Widget.search("foo @publish_ at > #{Time.now}",:match _mode=>:extended' 的语法是什么?是这样的吗甚至可能吗?

I have Widget.title, Widget.publish_ at, and Widget.unpublish_ at. It's a rails app with thinking_sphinx running, indexing once a night. I want to find all Widgets that have 'foo' in the title, and are published (publish _at < Time.now, unpublish _at > Time.now).

To get pagination to work properly, I really want to do this in a sphinx query. I have
'has :publish_at, :unpublish_at' to get the attributes, but what's the syntax for 'Widget.search("foo @publish_ at > #{Time.now}",:match _mode=>:extended'? Is this even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-07-16 10:05:38

是的,很容易做到,只需确保您覆盖了索引中的时间即可:

class Widget < ActiveRecord::Base
  define_index do
    indexes title
    has publish_at
    has unpublish_at
    ...
  end

要纯粹根据日期来获取它,需要少量的技巧,因为狮身人面像需要有界范围(x..y而不是x>) ;=y)。 最小/最大值的使用非常不优雅,但我目前不知道有什么好的方法可以解决它。

min_time = Time.now.advance(:years => -10)
max_time = Time.now.advance(:years => 10)
title = "foo"

Widget.search title, :with => {:publish_at => min_time..Time.now, :unpublish_at => Time.now..max_time}

Yep, easily possible, just make sure you're covering the times in your indexes:

class Widget < ActiveRecord::Base
  define_index do
    indexes title
    has publish_at
    has unpublish_at
    ...
  end

To pull it based purely off the dates, a small amount of trickery is required due to sphinx requiring a bounded range (x..y as opposed to x>=y). The use of min/max value is very inelegant, but I'm not aware of a good way around it at the moment.

min_time = Time.now.advance(:years => -10)
max_time = Time.now.advance(:years => 10)
title = "foo"

Widget.search title, :with => {:publish_at => min_time..Time.now, :unpublish_at => Time.now..max_time}
捶死心动 2024-07-16 10:05:38

我还没有使用过带有rails的sphinx。
但这可以通过 Sphinx API 实现。
您需要做的是在 sphinx.conf 中设置日期时间属性。
并且不要忘记在索引选择中使用 UNIX_TIMESTAMP(publish_at)、UNIX_TIMESTAMP(unpublish_at)。

I haven't used sphinx with rails yet.
But this is possible by the Sphinx API.
What you need to do is to set a datetime attribute at your sphinx.conf.
And don't forget to use UNIX_TIMESTAMP(publish_at), UNIX_TIMESTAMP(unpublish_at) at your index select.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文