使用 Rails 3.0 中的 Think Sphinx 搜索开放和关闭营业时间

发布于 2024-10-27 04:07:11 字数 467 浏览 1 评论 0原文

我有一个有趣的问题,也许有人可以解决。我有一个想要搜索的商业模式,所以我将 Thinking_sphinx 连接到其中,现在我可以搜索了。

现在我的商业模式有很多小时。

class Business
has_many :hours
define_index ...

class Hour
belongs_to :business

我的小时表包含以下列:business_id、day_of_week(int)、open_time(mysql time)、close_time(mysql_time)。使问题更加复杂的另一个问题是每天可能有多个开放/关闭时间。例如:星期一营业时间 8:00-12:15 和 15:00-20:00

如果我想在我的网站上添加过滤器以仅在搜索中显示营业中的企业,有没有办法可以将我的营业时间索引到sphinx 索引并通过 :with? 进行搜索

有人知道如何解决这个问题吗?

I have an interesting problem that maybe someone can figure out. I have a business model that I want to search through, so I hook in thinking_sphinx into it and now I can search.

Now my business model has_many hours.

class Business
has_many :hours
define_index ...

class Hour
belongs_to :business

My hours table contains the following columns: business_id, day_of_week(int), open_time(mysql time), close_time(mysql_time). One other issue to compound the problem is that there could be multiple open/close times for each day. For example: open Monday 8:00-12:15 and 15:00-20:00

If I want to add a filter on my website to only display open businesses in the search, is there a way I can index my hours into the sphinx index and search via :with?

Anyone got any ideas how to tackle this problem?

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

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

发布评论

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

评论(2

初见你 2024-11-03 04:07:11

这可能并不理想,但您应该能够进行搜索 - 但是,它将通过小时模型而不是业务模型进行。

define_index do
  indexes business.name, :as => :business

  has business_id
  has day_of_week
  has 'CAST(open_hour AS INT)',  :as => :open_hour,  :type => :integer
  has 'CAST(close_hour AS INT)', :as => :close_hour, :type => :integer
end

我不确定时间的转换 - 但本质上我们只想处理整数,Sphinx 没有时间数据类型。

搜索可能看起来像这样:

now  = Time.zone.now
time = (now - now.beginning_of_day).to_i
Hour.search 'foo',
  :with => {
    :day_of_week => now.wday,
    :open_hour   => 0..time,
    :close_hour  => time..240000
  },
  :group_by       => 'business_id',
  :group_function => :attr

这将为您提供按开放的企业 ID 分组的小时对象 - 因此您不会获得重复的企业。

所有理论,但尝试一下,让我知道你进展如何:)

This probably isn't ideal, but you should be able to get a search working - however, it'll be via the Hour model, rather than Business.

define_index do
  indexes business.name, :as => :business

  has business_id
  has day_of_week
  has 'CAST(open_hour AS INT)',  :as => :open_hour,  :type => :integer
  has 'CAST(close_hour AS INT)', :as => :close_hour, :type => :integer
end

I'm not certain about the casting of the times - but essentially we just want to be dealing with integers, Sphinx has no time data type.

Searching will probably look something like this:

now  = Time.zone.now
time = (now - now.beginning_of_day).to_i
Hour.search 'foo',
  :with => {
    :day_of_week => now.wday,
    :open_hour   => 0..time,
    :close_hour  => time..240000
  },
  :group_by       => 'business_id',
  :group_function => :attr

This will get you hour objects grouped by business id that are open - so you won't get duplicate businesses.

All theory, but give it a spin, let me know how you go :)

夏末 2024-11-03 04:07:11

首先,您需要将 Hours 模型更改为“belongs_to”而不是 has_one,以便 Hours 模型知道它直接链接到所附加的业务。您可能还希望将 open_time、close_time 和 day_of_week 列一起索引,以便可以快速一起搜索它们。

add_index :hours, [:day_of_week, :open_time, :close_time]

为了找到开放的业务,您可以创建如下所示的范围。我假设您正在使用 ActiveRecord。

class Hour < ActiveRecord::Base
  belongs_to :business

  scope :open, lambda { |day, time| { :conditions => ["hours.day_of_week = ? AND hours.open_time >= ? AND hours.close_time < ?", day, time, time] } }
end

通过这种方式,您只需执行以下操作即可找到开放的企业:

Hour.open.includes(:business)

每个开放的企业都将附加到其营业时间模型。如果您想迭代它们并访问企业信息,您可以这样做:

Hour.open.includes(:business).each do |hour|
  puts "#{hour.business.name} is open! It closes at #{hour.close_time}."
end

First off you'll need to change your Hours model to "belongs_to" instead of has_one, so that the Hours model will know it directly links to the business that is attached to. You'll probably also want to have the open_time, close_time, and day_of_week columns indexed together, so that you can quickly search them together.

add_index :hours, [:day_of_week, :open_time, :close_time]

In order to find the open businesses, you could make a scope that would look like the following. I'm assuming you're using ActiveRecord.

class Hour < ActiveRecord::Base
  belongs_to :business

  scope :open, lambda { |day, time| { :conditions => ["hours.day_of_week = ? AND hours.open_time >= ? AND hours.close_time < ?", day, time, time] } }
end

This way to find the open businesses you would just have to do:

Hour.open.includes(:business)

And each of the businesses that are open will be there attached to their Hours model. If you wanted to iterate over them and access the business's info, you could do:

Hour.open.includes(:business).each do |hour|
  puts "#{hour.business.name} is open! It closes at #{hour.close_time}."
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文