How to use Multi-Value Attributes (MVA) and facets together in Thinking Sphinx?
Asked Answered
T

1

7

I've got rails 3 app with models:

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

I need to filter events by categories and render category tree with counts:

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
...

I tried to do like this:

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

This code generates cool search results but facet counts are sad:

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

Instead of:

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

And the funniest part started when I decided to change index, but forgot to rebuild it:

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

This is a dirty hack: sphinx starts to use old index query with new model logic. Counts and search results are great. But, of course, if we will try to rebuild indexes, deploy or smth, then counts will be broken again.

The question is: how to use MVA with facets together?

Found questions from '09, with the same problem: http://www.mailinglistarchive.com/[email protected]/msg00473.html http://groups.google.com/group/thinking-sphinx/browse_thread/thread/e06cfab6aad327d2

Thank you.

Tenaille answered 22/3, 2012 at 11:53 Comment(0)
E
2

Add :all_ints => true to that attribute definition.

I had the same issue and fixed it. Please check it here:

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

It might be useful to you.

Empanel answered 14/3, 2014 at 10:52 Comment(1)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.Graaf

© 2022 - 2024 — McMap. All rights reserved.