2012-03-22 19 views
7

我有轨3应用与模式:如何在Thinking Sphinx中一起使用多值属性(MVA)和Facet?

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 

这是一个肮脏的黑客:斯芬克斯开始使用老带有新模型逻辑的索引查询。计数和搜索结果非常好。但是,当然,如果我们将尝试重建索引,部署或失败,那么计数将再次被破坏。

问题是:如何将MVA与facet一起使用?从09

发现的问题,同样的问题:http://www.mailinglistarchive.com/[email protected]/msg00473.html http://groups.google.com/group/thinking-sphinx/browse_thread/thread/e06cfab6aad327d2

谢谢。

回答

2

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

我有同样的问题,并修复它。请点击此处查看:

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

这可能是对你有用。

+1

请注意,[只有链接的答案](http://meta.stackoverflow.com/tags/link-only-answers/info)不鼓励,所以答案应该是搜索解决方案的终点(与另一个引用的中途停留时间相比,这些引用往往会随着时间推移而变得过时)。请考虑在此添加独立的摘要,并将链接保留为参考。 – kleopatra 2014-03-14 10:54:01

相关问题