Ola Karlsson
Ola Karlsson

Reputation: 107

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

Thanks

/ Ola

Upvotes: 0

Views: 1056

Answers (3)

DucDigital
DucDigital

Reputation: 4622

Does this answer your question:

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

Upvotes: 0

Harish Shetty
Harish Shetty

Reputation: 64363

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")

Upvotes: 1

Tony Fontenot
Tony Fontenot

Reputation: 5101

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}

Upvotes: 0

Related Questions