Search code examples
ruby-on-railssphinxthinking-sphinxacts-as-taggable

Thinking Sphinx and acts_as_taggable_on plugin


I installed Sphinx and Thinking Sphinx for ruby on rails 2.3.2.

When I search without conditions search works ok. Now, what I'd like to do is filter by tags, so, as I'm using the acts_as_taggable_on plugin, my Announcement model looks like this:

class Announcement < ActiveRecord::Base

  acts_as_taggable_on :tags,:category

  define_index do
    indexes title, :as => :title, :sortable => true
    indexes description, :as => :description, :sortable => true
    indexes tags.name, :as => :tags
    indexes category.name, :as => :category

    has category(:id), :as => :category_ids
    has tags(:id), :as => :tag_ids
  end

For some reason, when I run the following command, it will bring just one announcement, that has nothing to do with what I expect. I've got many announcements, so I expected a lot of results instead.

Announcement.search params[:announcement][:search].to_s, :with => {:tag_ids => 1}, :page => params[:page], :per_page => 10

I guess something is wrong, and it's not searching correctly.

Can anyone give my a clue of what's going on?

Thanks, Brian


Solution

  • Thinking Sphinx relies on associations in model. In common situations you only have to put index definition below your associations.

    With acts_as_taggable_on plug-in you don't have tag-related associations in model file and when you write

    indexes tags.name, :as => :tags

    TS interprets it like:

    CAST(`announcements`.`name` AS CHAR) AS `tags`
    

    (look at sql_query in development.sphinx.conf, in my case). I suppose that you have attribute name in model Announcement and don't run into error when rebuild index.

    But we expect:

    CAST(GROUP_CONCAT(DISTINCT IFNULL(`tags`.`name`, '0') SEPARATOR ' ') AS CHAR) AS `tags`
    

    and:

    LEFT OUTER JOIN `taggings` ON (`announcements`.`id` = `taggings`.`taggable_id`)  
    LEFT OUTER JOIN `tags` ON (`tags`.`id` = `taggings`.`tag_id`) AND taggings.taggable_type = 'Announcement'
    

    To get things working just add tag-related associations in your model before you rebuild index:

    class Announcement < ActiveRecord::Base
    
      acts_as_taggable_on :tags,:category
    
      has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag, :class_name => "ActsAsTaggableOn::Tagging",
                :conditions => "taggings.taggable_type = 'Announcement'"
      #for context-dependent tags:
      has_many :category_tags, :through => :taggings, :source => :tag, :class_name => "ActsAsTaggableOn::Tag",
              :conditions => "taggings.context = 'categories'"
    

    In define_index method:

    indexes category_tags(:name), :as => :tags
    has category_tags(:id), :as => :tag_ids, :facet => true
    

    In controller:

    @announcement_facets = Announcement.facets params[:search], :with => {:tag_ids => [...]} 
    @announcements = @announcement_facets.for.paginate( :page => params[:page], :per_page => 10 )