7

我有带有模型的 rails 3 应用程序:

class Event < ActiveRecord::Base
  has_many :event_categories
  has_many :categories, through: :event_categories
end

class EventCategory < ActiveRecord::Base
  belongs_to :category
  belongs_to :event
end

class Category < ActiveRecord::Base
  belongs_to :parent, class_name: 'Category'
  has_many :subcategories, class_name: 'Category', foreign_key: :parent_id
end

我需要按类别过滤事件并使用计数呈现类别树:

Music Festivals (10) # id: 1
-- Classic (2)       # id: 3
-- Pop (8)           # id: 8
IT Conferences (2)   # id: 10
-- Ruby (1)          # id: 11
-- PHP (1)           # id: 12
...

我试着这样做:

define_index do
  has category_values, type: :multi, facet: true
end

before_save :collect_category_values

def collect_category_values
  # traversing events categories
  # putting string like '10/1/3' to self.category_values 
  # which contains all event's categories and subcategories
end

这段代码生成了很酷的搜索结果,但方面的数量很可悲:

{ :category_values => { '1/3' => 2, '10/11' => 1 } }

代替:

{ :category_values => { 1 => 10, 3 => 2, 10 => 2, 11 => 1 }

最有趣的部分开始于我决定更改索引但忘记重建它时:

# old attribute --> has category_values, type: :multi, facet: true
has categories(:id), as: :category_id, type: :multi facet: true

这是一个肮脏的 hack:sphinx 开始使用旧的索引查询和新的模型逻辑。计数和搜索结果都很棒。但是,当然,如果我们尝试重建索引、部署或 smth,那么计数将再次被打破。

问题是:如何将 MVA 与 facets 一起使用?

发现 09 年的问题,同样的问题: http: //www.mailinglistarchive.com/thinking-sphinx@googlegroups.com/msg00473.html http://groups.google.com/group/thinking-sphinx/browse_thread/线程/e06cfab6aad327d2

谢谢你。

4

1 回答 1

2

:all_ints => true添加到该属性定义中。

我有同样的问题并修复了它。请在此处查看:

https://github.com/pat/thinking-sphinx/issues/357

它可能对你有用。

于 2014-03-14T10:52:41.537 回答