开发者

Dynamic fields with Thinking Sphinx

I'm building an application where I have products and categories. Category has_many properties and each property has a list of possible values. After a category is set to the product all properties show up in the form and the user can set that property to one of the properties possible values.

My question is:

Is it possible for Thinking Sphinx to filter the products through a property and property value ex:

:with => {:property_id => property_value}

If it's possibl开发者_如何学JAVAe, what is the best way to implement this? If not is there any other library out there to solve this problem?

Thanks

/ Ola


One approach is to store the property_id as multi-value attribute.

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

Now you can issue following search commands:

Product.search_with_properties("XYZ", nil, :region => "South West")


Try this:

Add the following to your define_index:

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

Then you can use :with / :without like:

:with => {:property_ids => property_value}


Does this answer your question:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜