0

我正在构建一个包含产品和类别的应用程序。类别 has_many 属性,每个属性都有一个可能值列表。将类别设置为产品后,所有属性都会显示在表单中,用户可以将该属性设置为属性可能值之一。

我的问题是:

Thinking Sphinx 是否可以通过属性和属性值过滤产品,例如:

:with => {:property_id => property_value}

如果可能,实现这一点的最佳方法是什么?如果没有,还有其他图书馆可以解决这个问题吗?

谢谢

/ 奥拉

4

3 回答 3

1

一种方法是将 property_id 存储为多值属性。

class Product < ActivRecord::Base
  has_one :category
  has_many :properties, :through => :category

  KVP = "###"
  define_index do
    has properties("CONCAT(`properties`.`key`, \"%s\", `properties`.`value`)" %
           KVP, :as => :category_key_value
  end

  def search_with_properties keys, with_attr={}, p={}
    wp = (with_attr||{}).dup
    values = p.map{|k, v| "#{k}#{KVP}#{v}"} unless p.empty?
    wp = wp.merge({:category_key_value => values}) unless values.empty?
    search keys, :with => wp
  end
end

class Category < ActivRecord::Base
  belongs_to :product
  has_many :properties
end

class Property < ActivRecord::Base
  belongs_to :Category
  #key    E.g: region
  #value  E.g: South West
end

现在您可以发出以下搜索命令:

Product.search_with_properties("XYZ", nil, :region => "South West")
于 2010-04-21T22:22:39.297 回答
0

尝试这个:

将以下内容添加到您的define_index:

has properties(:id), :as => :property_ids

然后你可以使用:with/:without喜欢:

:with => {:property_ids => property_value}
于 2010-04-21T21:19:07.573 回答
0

这回答了你的问题了吗:

https://github.com/freelancing-god/thinking-sphinx/issues/356

于 2012-05-07T07:03:12.937 回答