1

使用这些关联,只有放大结果会返回正确的结果,但是当我尝试搜索第二个关联时,它将返回 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
4

4 回答 4

1

你能在sql_query里面分享为你的产品模型生成的development.sphinx.conf吗?确实,您所做的应该对这两个属性都有效,因此生成的 SQL 命令中可能存在错误。

于 2009-10-15T21:38:58.937 回答
0

我想出了一个解决方法,直到 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 NULL
于 2009-10-16T16:17:17.150 回答
0

狮身人面像的东西大多是正确的。搜索中的 nil 是多余的。但老实说,我认为这不是导致您问题的原因。

我认为您的问题源于您的模型关系。特别是你的单表继承 (STI) 版本,以及 Sphinx 处理索引的方式。

看来您实际上是在尝试复制索引,因此它忽略了第二个索引。

我不完全确定如何修复它,将 where 查询添加到 define_index 块不起作用,因为所有 where 语句都适用于之后定义的所有索引并且不能被覆盖,只能添加到。Thinking sphinx 也不适用于模型上的多个索引,因此您无法通过重新定义 define_index 块来修复它。

Thinking-Sphinx 1.2 提供了Sphinx Scopes,您可能希望将其视为潜在的解决方案。不幸的是,它的记录很差,所以我不知道它是否会起作用。

鉴于 Thinking-Sphinx 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-15T20:53:17.480 回答
0

我使用捷径方法修复它,不确定它是否可以...我定义了第三个关系,其中包括我之前定义的关系,只是为了搜索并在 define_index 方法中使用它。你可以在这里看到我的关系- http://stackoverflow.com/questions/15791007/thinking-sphinx-search-for-different-conditions-from-the-same-join-table/15804611#15804611

于 2013-04-04T07:17:01.953 回答