对于这些关联,只有放大结果返回正确的结果,但当我尝试搜索第二个关联时,它将返回0结果。
has_one :magnification,
:class_name => 'ProductAttribute',
:foreign_key => 'product_id',
:conditions => {:key => 'Magnification'}
has_one :objective_lens,
:class_name => 'ProductAttribute',
:foreign_key => 'product_id',
:conditions => {:key => 'Objective Lens Diameter'}
define_index do
has magnification(:value), :type => :float, :as => :magnification
has objective_lens(:value), :type => :float, :as => :objective_lens_diameter
end使用样本代码
# returns expected results
Product.search(nil, :with => {:magnification => (8.0..9.0)})
# returns 0 results
Product.search(nil, :with => {:objective_lens_diameter => (31.0..61.0)})但是,当我反转define_index的顺序时,情况正好相反。因此物镜直径结果返回正确的结果,放大结果返回0。
使用Rails v2.2,Thinking-Sphinx作为插件v1.2.12和Sphinx 0.9.8
编辑:查看生成的sql_query值显示第二个属性的联接使用了错误的关联,因此不会返回预期的结果。
简化结果:
SELECT
`products`.`id` * 2 + 1 AS `id`,
`products`.`id` AS `sphinx_internal_id`,
1234567890 AS `class_crc`,
`product_attributes`.`value` AS `magnification`,
`objective_lens_products`.`value` AS `objective_lens_diameter`
FROM `products`
LEFT OUTER JOIN `product_attributes` ON product_attributes.product_id = products.id
AND `product_attributes`.`key` = 'Magnification'
LEFT OUTER JOIN `product_attributes` objective_lens_products ON objective_lens_products.product_id = products.id
AND `product_attributes`.`key` = 'Objective Lens Diameter'
WHERE `products`.`id` >= $start
AND `products`.`id` <= $end
GROUP BY `products`.`id`
ORDER BY NULL发布于 2009-10-15 21:38:58
您能在sql_query中共享为您的产品模型生成的development.sphinx.conf吗?实际上,您所做的应该同时适用于这两个属性,因此生成的SQL命令中可能有一个bug。
发布于 2009-10-15 20:53:17
斯芬克斯的东西大部分都是对的。搜查中的尼尔斯是多余的。但我真的不认为这是导致你的问题的原因。
我认为你的问题源于你的模特关系。特别是合并了单一表继承(STI)的版本,以及Sphinx处理索引的方式。
看起来,您实际上是在复制一个索引,因此它忽略了第二个索引。
我不完全确定如何修复它,向define_index块添加了where查询,因为所有where语句都应用于事后定义的所有索引,不能被过写,只能添加到其中。对于模型上的多个索引,sphinx也不能很好地工作,所以不能通过重新定义define_index块来修复它。
Thinking-Sphinx1.2提供了狮身人面像,您可能希望将其作为一种潜在的解决方案来研究。不幸的是,它的文档很少,所以我无法判断它是否会起作用。
考虑到谷歌的思想缺乏点击率--狮身人面像、STI和“单一表继承”。我希望重新定义您的关系,让rails处理STI,以解决您的问题。
它涉及到这样的事情:
class ProductAttribute < ActiveRecord::Base
belongs_to :product
inheritance_column => :key
...
common methods and validations to objective\_lens\_diameters and and magnifications
...
end
class Magnification < ProductAttribute
...
methods and validations unique to magnifications.
...
end
class ObjectLensDiameter < ProductAttribute
...
methods and validations unique to object lens diameters
...
end
class Product < ActiveRecord::Base
has_one :magnification
has_one :objective_lens
define_index do
has magnification(:value), :type => :float, :as => :magnification
has objective_lens(:value), :type => :float, :as => :objective_lens_diameter
end
end您可能需要迁移,以使ProductAttributes表中的现有键值与STI期望的值一致。
发布于 2009-10-16 16:17:17
在sql_query协会被修复之前,我一直在考虑一项工作。性能比使用左联接差,但同时也减少了使其工作所需的外部代码量。
因此,定义索引被更改为使用SQL片段直接获取特定列,而不是依赖于任何联接。
define_index do
has "(SELECT `value` " +
"FROM `product_attributes` " +
"WHERE `product_id` = `products`.`id` " +
" AND `key` = 'Magnification' " +
"LIMIT 0, 1)",
:type => :float,
:as => :magnification
has "(SELECT `value` " +
"FROM `product_attributes` " +
"WHERE `product_id` = `products`.`id` " +
" AND `key` = 'Objective Lens Diameter' " +
"LIMIT 0, 1)",
:type => :float,
:as => :objective_lens_diameter
end生成sql_query
SELECT `products`.`id` * 2 + 1 AS `id`,
`products`.`id` AS `sphinx_internal_id`,
123456789 AS `class_crc`,
IFNULL('987654321', 0) AS `subclass_crcs`,
0 AS `sphinx_deleted`,
(SELECT `value`
FROM `product_attributes`
WHERE `product_id` = `products`.`id`
AND `key` = 'Magnification'
LIMIT 0, 1) AS `magnification`,
(SELECT `value`
FROM `product_attributes`
WHERE `product_id` = `products`.`id`
AND `key` = 'Objective Lens Diameter'
LIMIT 0, 1) AS `objective_lens_diameter`
FROM `products`
WHERE `products`.`id` >= $start
AND `products`.`id` <= $end
GROUP BY `products`.`id`
ORDER BY NULLhttps://stackoverflow.com/questions/1573144
复制相似问题