Thinking Sphinx 的动态字段

发布于 2024-08-30 03:38:02 字数 298 浏览 2 评论 0原文

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

我的问题是:

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

:with => {:property_id => property_value}

如果可能,实现此目的的最佳方法是什么?如果没有,还有其他库可以解决这个问题吗?

谢谢

/奥拉

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好倦 2024-09-06 03:38:02

一种方法是将 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")

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")
冷︶言冷语的世界 2024-09-06 03:38:02

试试这个:

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

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

然后您可以使用 :with / :without ,例如:

:with => {:property_ids => property_value}

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文