思考sphinx范围从范围输入中产生意外结果

思考sphinx范围从范围输入中产生意外结果,sphinx,thinking-sphinx,Sphinx,Thinking Sphinx,我有一个活动记录作用域,我正试图在sphinx_作用域中复制该作用域: scope :active, -> (date) { where("DATE(?) BETWEEN active_date AND hide_date-1 ", date) } # between is inclusive 这对于政府工作来说已经足够接近了,除非有人能给我一个更好的方法: sphinx_scope(:search_active) { |today| {:with => {:active_da

我有一个活动记录作用域,我正试图在sphinx_作用域中复制该作用域:

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}
  }
(是的,今天。到美国到时间。到我有点尴尬…)

我的问题是结果似乎不准确。例如,没有范围的查询将产生

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

有人能解释一下这里发生了什么吗

[编辑]---------------------------------------------------------

好的,使用@DGM,我发现sphinx数据库记录中的hide_date值不正确。对于记录id 3633,mysql数据库记录908的日期值为2115-07-15

'2115-07-15'.to_time.to_i
=> 4592610000 
显然,在
297642704
4592610000
之间存在一些差异

因此,我要么计算的范围不正确,要么sphinx数据库被错误填充

指数/标准指数.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时间戳之间的转换是否丢失了一些内容?

与其他时间范围相比,您的隐藏日期数据似乎太低:297642704

我不完全确定这里的问题是什么,但有一些想法:

如果您将这些日期存储为字符串值,那么将传递给Sphinx。
:type
设置仅用于生成Sphinx配置文件,而不用于转换属性值(当然,有一个强有力的理由是它们应该同时执行这两项操作!)。当然,这并不能完全解释2115-07-15是如何变成297642704的,但可能是其中的一部分。同样,仅当模型以字符串而不是日期返回
hide_date

另一个问题是Sphinx将时间戳存储为无符号整数,因此应避免1970年1月1日之前的任何情况

这不是问题的解决方案,但可能是总体问题的解决方案:我建议在Sphinx中使用日期本身的整数表示。e、 g.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值更容易调试

你确实是对的。谢谢你的关注。希望Pat看到这一点,并解决我是否计算时间不正确或sphinx数据库填充不正确的问题。很好的解决方法!这使得故障排除变得更加容易。我使用的是mysql
date
字段,但我想你是对的,它一定是模型中的字符串。我觉得很有趣,因为我尽量避免使用日期作为字符串进行排序,因为它对大多数格式都失败了。。。但这是本例中解决方案的一部分。老实说,我有点惊讶它在某处被当作字符串处理——Rails和TS/Riddle都应该把它当作日期实例来处理。不过,这是否意味着现在一切都按预期进行?将其转换为整数既能按预期进行,也更容易理解。双赢。不确定是什么出了问题,但这很有效:)