1

我有一个应用程序,我需要在相关模型的一个字段上实现分面搜索功能,但它似乎不起作用。这是一个简短的背景:我正在研究 3 个模型:1. 产品 2. 属性 3. 产品属性。请参阅下面的代码片段:

 class Product < ActiveRecord::Base
      ...
      has_many :attribute_products
      has_many :attributes, :through => :attribute_products, :class_name => 'Attribute'

      has_one :brand, :class_name => "ProductAttribute", :conditions => "product_attributes.attribute_id IN (Select id from attributes where name = 'Brand')"
      has_one :model, :class_name => "ProductAttribute", :conditions => "product_attributes.attribute_id IN (Select id from attributes where name = 'Model')"
      ....

      define_index do
        indexes :name
        indexes description

        indexes brand(:attribute_value), :as => :brand, :facet => true
        indexes model(:attribute_value), :as => :model,  :facet => true

       has product_category_id, :type => :integer, :facet => true

        where "products.online = 1 AND products.product_category_id IN (SELECT id FROM product_categories WHERE product_categories.parent_category_id IS NOT NULL)"
      end
    ...
    end
    -----------------------------------------------------------------------
    class AttributeProduct < ActiveRecord::Base
      # => Since attribute is already taken, renaming the association method to prod_attr
      belongs_to :attribute, 
      belongs_to :product
      ...
    end
    ------------------------------------------------------------------
    class Attribute < ActiveRecord::Base
      has_many :attribute_products
      has_many :products, :through => :attribute_products
      ....
    end
    ------------------------------------------------------------------

我已经定义了“产品”和“品牌”之间以及“产品”和“模型”之间的 1:1 关系。然后我在两者上定义索引并使它们成为构面。

现在在 Product 模型中,搜索品牌会返回结果,但搜索模型名称不会返回任何结果。

Product.search(:conditions => {:model => "U2716"})
 Sphinx   Querying: '@model 1144055474'
 Sphinx (0.002063s)   Found 0 result
-------------------------------------------------------------------
 Product.search(:conditions => {:brand => "Calvin Klein"})
  Sphinx   Querying: '@brand Calvin'
  Sphinx (0.004142s)   Found 7 results
-------------------------------------------------------------------

据我了解,“模型”内容正在被编入索引。结果如下:

Product.facets(:conditions => {:brand => "Calvin"})
{:model=>{"U2716"=>7}, :product_category_id=>{6=>1, 2=>6}, :cost=>{1=>3, 2=>4}, :brand=>{"Calvin Klein"=>7}}
---------------------------------------------------------------------
>> Product.facets(:conditions => {:model => "U2716"})
{:model=>{}, :product_category_id=>{}, :cost=>{}, :brand=>{}}

不知道我错过了什么?

4

0 回答 0