思维狮身人面像范围从范围输入中产生意想不到的结果
thinking sphinx scope yielding unexpected results from range input
我有一个活动记录范围,我正试图在 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}
}
(是的,today.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) |
我认为这实际上不是 sphinx 的思维错误,而是 sphinx 本身的问题。或者,更有可能......我误解了一些东西:p
任何人都可以阐明这里发生的事情吗?
[编辑] -------------------------------------- --------------
好的,通过@DGM,我发现 hide_date 的值在 sphinx 数据库记录中不正确。对于记录 id 3633,mysql 数据库记录 908 的日期值为 2115-07-15。
'2115-07-15'.to_time.to_i
=> 4592610000
显然,297642704
和 4592610000
之间存在一些差异。
所以我要么计算范围不正确,要么填充 sphinx 数据库时出错。
indices/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 时间戳之间的转换中丢失了一些东西?
与其他时间范围相比,您的 hide_date 数据似乎太低:297642704
我不完全确定这里的问题是什么,但有一些想法:
如果您将这些日期存储为字符串值,那么这就是将传递给 Sphinx 的内容。 :type
设置仅用于生成 Sphinx 配置文件,而不用于转换属性值(当然,有一个强有力的论据认为它们应该同时进行!)。当然,这并不能完全解释 2115-07-15 是如何变成 297642704 的,但可能是其中的一部分。同样,仅当模型返回 hide_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 值更易于调试!
我有一个活动记录范围,我正试图在 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}
}
(是的,today.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) |
我认为这实际上不是 sphinx 的思维错误,而是 sphinx 本身的问题。或者,更有可能......我误解了一些东西:p
任何人都可以阐明这里发生的事情吗?
[编辑] -------------------------------------- --------------
好的,通过@DGM,我发现 hide_date 的值在 sphinx 数据库记录中不正确。对于记录 id 3633,mysql 数据库记录 908 的日期值为 2115-07-15。
'2115-07-15'.to_time.to_i
=> 4592610000
显然,297642704
和 4592610000
之间存在一些差异。
所以我要么计算范围不正确,要么填充 sphinx 数据库时出错。
indices/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 时间戳之间的转换中丢失了一些东西?
与其他时间范围相比,您的 hide_date 数据似乎太低:297642704
我不完全确定这里的问题是什么,但有一些想法:
如果您将这些日期存储为字符串值,那么这就是将传递给 Sphinx 的内容。 :type
设置仅用于生成 Sphinx 配置文件,而不用于转换属性值(当然,有一个强有力的论据认为它们应该同时进行!)。当然,这并不能完全解释 2115-07-15 是如何变成 297642704 的,但可能是其中的一部分。同样,仅当模型返回 hide_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 值更易于调试!