1

これらの関連付けでは、拡大結果のみが正しい結果を返しますが、2番目の関連付けを検索しようとすると、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およびSphinx0.9.8として使用

編集:生成されたsql_query値を見ると、2番目の属性の結合が間違った関連付けを使用しているため、期待される結果が返されません。

簡略化された結果:

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

スフィンクスのものはほとんど正しいです。検索のnilは不要です。しかし、私は正直なところ、それがあなたの問題を引き起こしているとは思いません。

あなたの問題はあなたのモデルの関係から生じていると思います。特に、ハッキングされたバージョンの単一テーブル継承(STI)と、Sphinxがインデックスを処理する方法。

基本的にインデックスを複製しようとしているようですので、2番目のインデックスは無視されます。

修正方法が完全にはわかりません。whereクエリをdefine_indexブロックに追加しても機能しません。これは、すべてのwhereステートメントが後で定義されたすべてのインデックスに適用され、上書きできず、追加されるだけだからです。スフィンクスを考えることは、モデル上の複数のインデックスでもうまく機能しないため、define_indexブロックを再定義して修正することはできません。

Thinking-Sphinx 1.2は、潜在的なソリューションとして検討したいSphinxスコープを提供します。残念ながら、それは十分に文書化されていないので、それが機能するかどうかはわかりません。

Thinking-SphinxSTIおよび「単一テーブル継承」のグーグルに関するヒットが不足していることを考えると。私はあなたの関係を再定義し、レールに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

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

ショートカットメソッドを使用して修正しましたが、問題がないかどうかはわかりません...検索のためだけに以前に定義したリレーションとdefine_indexメソッドで使用したリレーションの両方を含む3番目のリレーションを定義しました。ここで私の関係を見ることができます- http://stackoverflow.com/questions/15791007/thinking-sphinx-search-for-different-conditions-from-the-same-join-table/15804611#15804611

于 2013-04-04T07:17:01.953 に答える