Nic*_*nov 7 search ruby-on-rails thinking-sphinx facets
我有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
Run Code Online (Sandbox Code Playgroud)
我需要按类别过滤事件并使用计数呈现类别树:
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
...
Run Code Online (Sandbox Code Playgroud)
我试着这样做:
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
Run Code Online (Sandbox Code Playgroud)
此代码生成很酷的搜索结果,但方面计数很难过:
{ :category_values => { '1/3' => 2, '10/11' => 1 } }
Run Code Online (Sandbox Code Playgroud)
代替:
{ :category_values => { 1 => 10, 3 => 2, 10 => 2, 11 => 1 }
Run Code Online (Sandbox Code Playgroud)
最有趣的部分是在我决定更改索引时开始的,但忘了重建它:
# old attribute --> has category_values, type: :multi, facet: true
has categories(:id), as: :category_id, type: :multi facet: true
Run Code Online (Sandbox Code Playgroud)
这是一个肮脏的黑客:sphinx开始使用旧的索引查询与新的模型逻辑.计数和搜索结果都很棒.但是,当然,如果我们尝试重建索引,部署或smth,那么计数将再次被打破.
问题是:如何将MVA与facet结合使用?
从'09发现问题,同样的问题:http ://www.mailinglistarchive.com/thinking-sphinx@googlegroups.com/msg00473.html http://groups.google.com/group/thinking-sphinx/browse_thread/线程/ e06cfab6aad327d2
谢谢.
将:all_ints => true添加到该属性定义中。
我有同样的问题并解决了它。请在这里检查:
它可能对你有用。
| 归档时间: |
|
| 查看次数: |
1368 次 |
| 最近记录: |