如何在太阳黑子可搜索块中动态构造属性名称
我的搜索包括按价格范围、星期几和客人数量确定查询范围的功能。
但是,一周中每一天的价格都不同,并且价格会根据客人数量而变化(非线性)。我能想到的对数据进行索引的最佳方法是为每天/客人组合创建一个不同的字段,例如,
searchable do
integer :price_sunday_2_guests do
price(:sunday, 2)
end
integer :price_sunday_3_guests do
price(:sunday, 3)
end
...
integer :price_monday_2_guests do
price(:monday, 2)
end
...
# and so on...
end
显然,我不想输入所有内容。我想在可搜索块中构造这些属性。更像是:
searchable do
Date::DAYNAMES.each do |day_name|
day = DateTime.strptime(day_name, '%A')
(guests_min..guests_max).each do |guests|
sym = (day_name.downcase + '_' + guests.to_s + 'guests_price_abs_max').to_sym
integer sym do
price(day_name, guests)
end
end
end
end
但我得到以下异常: NoMethodError: #
它不会抱怨常量 Date::DAYNAMES。
Sunspot 似乎试图将任何方法标记解释为字段。我想如果我能更好地理解它的话,这甚至是有意义的。
所以,我的问题是,有没有一种聪明的方法可以做到这一点?我是否需要硬编码一个范围并让我的价格方法返回空值?在我不知道的可搜索块中是否有一些可用的机制?
My search includes the ability to scope a query by a price range, the day of the week, and a guest count.
However, the price is different on each day of the week, and the price changes (not linearly) by the number of guests. The best way I can think of to index the data is to create a different field for each day/guest combo, e.g.,
searchable do
integer :price_sunday_2_guests do
price(:sunday, 2)
end
integer :price_sunday_3_guests do
price(:sunday, 3)
end
...
integer :price_monday_2_guests do
price(:monday, 2)
end
...
# and so on...
end
Obviously, I don't want to type all that in. I want to construct those attributes in the searchable blocks. More like:
searchable do
Date::DAYNAMES.each do |day_name|
day = DateTime.strptime(day_name, '%A')
(guests_min..guests_max).each do |guests|
sym = (day_name.downcase + '_' + guests.to_s + 'guests_price_abs_max').to_sym
integer sym do
price(day_name, guests)
end
end
end
end
But I get the following exception:
NoMethodError: undefined method `guests_min' for #<Sunspot::DSL::Fields:0x000001080bdcf0>
It does not complain about the constant Date::DAYNAMES.
It seems that Sunspot tries to interpret any method token as a field. Which, I suppose would even make sense if I understood it better.
So, my question, is there a smart way for me to do this? Do I need to just hardcode a range and let my price method return an empty value? Is there some mechanism available in the searchable block I'm not aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论