views:

151

answers:

2

Hi!

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 possible, what is the best way to implement this? If not is there any other library out there to solve this problem?

Thanks

/ Ola

A: 

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}
Tony Fontenot
It's a little more complex that that. I need to filter on the value of the property, not the property id.
Ola Karlsson
A: 

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")
KandadaBoggu
It seems that I don't have access to the method category_key_values when Thinking Sphinx indexes.Unknown column 'ads.category_key_values' in 'field list'
Ola Karlsson
Updated my answer, take a look.
KandadaBoggu