如何在Thinking Sphinx中使用多值属性(MVA)和方面?

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

谢谢.

Ran*_*avi 2

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

我有同样的问题并解决了它。请在这里检查:

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

它可能对你有用。

  • 请注意,不鼓励使用 [仅链接答案](http://meta.stackoverflow.com/tags/link-only-answers/info),因此答案应该是搜索解决方案的终点(而不是参考文献的另一个停留点,随着时间的推移,这些参考文献往往会变得陈旧)。请考虑在此处添加独立的概要,并保留链接作为参考。 (2认同)