我有一个活跃的记录范围,我试图在sphinx_scope中复制:
scope :active, -> (date) { where("DATE(?) BETWEEN active_date AND hide_date-1 ", date) } # between is inclusive
这足以让政府工作,除非有人能告诉我更好的方法:
sphinx_scope(:search_active) { |today|
{:with => {:active_date => 100.years.ago.to_i..today.to_s.to_time.to_i, :hide_date => today.to_s.to_time.to_i..100.years.from_now.to_i}
}
(是的,今天.to_s.to_time.to_i有点尴尬......)
我的问题是结果似乎不准确。例如,没有范围的查询会产生
sphinxQL> SELECT weight(),* FROM `standard_core` WHERE MATCH('1910.15') AND `sphinx_deleted` = 0 LIMIT 0, 10 OPTION field_weights=(section_number=2);
+----------+------+----------------+--------------------+-------------+-----------+------------+------------+-----------------------+-----------------------------------------------------------------------------------+---------------------+
| weight() | id | sphinx_deleted | sphinx_internal_id | active_date | hide_date | created_at | updated_at | sphinx_internal_class | title_sort | section_number_sort |
+----------+------+----------------+--------------------+-------------+-----------+------------+------------+-----------------------+-----------------------------------------------------------------------------------+---------------------+
| 8557 | 3633 | 0 | 908 | 1436936400 | 297642704 | 1451164539 | 1451164539 | Standard | § 1910.15 Shipyard employment. | 1910.15 |
| 6549 | 3637 | 0 | 909 | 1436936400 | 297642704 | 1451164539 | 1451164539 | Standard | § 1910.15(a) Adoption and extension of established safety and health... | 1910.15(a) |
| 6549 | 3641 | 0 | 910 | 1436936400 | 297642704 | 1451164539 | 1451164539 | Standard | § 1910.15(b) Definitions. For purposes of this section: | 1910.15(b) |
但是对于范围,缺少最相关的结果:
sphinxQL> SELECT weight() as weight,* FROM `standard_core` WHERE MATCH('1910.15') AND `active_date` BETWEEN -1672108252 AND 1482127200 AND `hide_date` BETWEEN 1482127200 AND 4639325348 AND `sphinx_deleted` = 0 ORDER BY weight DESC LIMIT 0, 10 OPTION field_weights=(section_number=2);
+--------+------+----------------+--------------------+-------------+------------+------------+------------+-----------------------+-----------------------------------------------------------------------------------+---------------------+
| weight | id | sphinx_deleted | sphinx_internal_id | active_date | hide_date | created_at | updated_at | sphinx_internal_class | title_sort | section_number_sort |
+--------+------+----------------+--------------------+-------------+------------+------------+------------+-----------------------+-----------------------------------------------------------------------------------+---------------------+
| 4566 | 5469 | 0 | 1367 | 1436936400 | 1484632800 | 1451167759 | 1451167759 | Standard | § 1910.27(d)(1)(vi) Ladder wells shall have a clear width of at least 15... | 1910.27(d)(1)(vi) |
| 4549 | 5413 | 0 | 1353 | 1436936400 | 1484632800 | 1451167757 | 1451167757 | Standard | § 1910.27(c)(2) Ladders without cages or wells. A clear width of at least 15... | 1910.27(c)(2) |
| 4549 | 5453 | 0 | 1363 | 1436936400 | 1484632800 | 1451167758 | 1451167758 | Standard | § 1910.27(d)(1)(ii) Cages or wells (except as provided in subparagraph (5) of... | 1910.27(d)(1)(ii) |
我不认为这实际上是一个思维的狮身人面像错误,而是与狮身人面像本身有关。或者,更可能......我误解的事情:p
有人能说清楚这里发生了什么吗?
[edit] -------------------------------------------- -------------
好的,使用@DGM,我发现sphinx数据库记录中hide_date的值不正确。对于记录id 3633,mysql数据库记录908具有日期值2115-07-15。
'2115-07-15'.to_time.to_i
=> 4592610000
显然,297642704
和4592610000
之间存在一些差异。
所以我要么错误地计算范围,要么正在填充错误的sphinx数据库。
指数/ standard_index.rb
ThinkingSphinx::Index.define :standard, :with => :real_time do
# fields
indexes title, :sortable => true
indexes content
indexes section_number, :sortable => true
# attributes
has active_date, :type => :timestamp
has hide_date, :type => :timestamp
has created_at, :type => :timestamp
has updated_at, :type => :timestamp
end
这是否会在mysql日期字段和sphinx时间戳之间的转换中丢失一些内容?
答案 0 :(得分:1)
我不完全确定这里的问题是什么,但有些想法:
如果您将这些日期存储为字符串值,那么这将被传递给Sphinx。 :type
设置仅用于生成Sphinx配置文件,而不是用于转换属性值(当然,他们应该同时执行两个强大的参数!)。当然,这并没有完全解释2115-07-15如何成为297642704,但可能是正在发挥作用的一部分。同样,仅当模型将hide_date
作为String返回而不是Date。
另一个问题是Sphinx将时间戳存储为无符号整数,因此应该避免在1970年1月1日之前的任何事情。
不是问题的解决方案,但可能是最重要的问题:我建议在Sphinx中使用日期本身的整数表示。例如2115-07-15成为21150715。
所以,你的模型中有如下内容:
def hide_date_to_i
hide_date.to_s.gsub('-', '').to_i
end
然后在索引定义中:
has hide_date_to_i, :as => :hide_date, :type => :integer
您还需要相应地更新范围。
希望这会让事情相应地发挥作用,但如果没有,至少应该让Sphinx值更容易调试!
答案 1 :(得分:0)
与其他时间范围相比,您的hide_date数据显得过低:297642704