Acts as taggable on and Thinking Sphinx Integration

Using thinking sphinx to search objects that are tagged using the acts_as_taggable_on plugin.

class Photo < ActiveRecord::Base
  ...
 
  acts_as_taggable_on :keywords
 
  define_index do
    indexes :caption
    indexes keywords.name, :as => :keywords
  end
 
  ...
end

Now lets assign some tags to our Photo

>> photo = Photo.create :user_id => 1, :caption => 'BMW M7', :keyword_list => 'BMW, Car'
>> photo.keywords
=> [#<Tag id: 1, name: "BMW">, #<Tag id: 3, name: "Car">]

Lets run the indexer

# rake thinking_sphinx:index

Now lets test it...

>> Photo.search 'Car'
=> [#<Photo id: 100, caption: "BMW M7", user_id: 1, publication_state: "Unpublished", created_at: "2009-11-20 00:00:47", updated_at: "2009-11-20 00:00:47", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>]
>> Photo.search('Car').first.keywords
=> [#<Tag id: 1, name: "BMW">, #<Tag id: 3, name: "Car">]

Search by tags only

>> Photo.search :conditions => {:keywords => 'Car'}
=> [#<Photo id: 100, caption: "BMW M7", user_id: 1, publication_state: "Unpublished", created_at: "2009-11-20 00:00:47", updated_at: "2009-11-20 00:00:47", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>]

2 comments

Gnomet's picture

Thanks for the post! I tried

Submitted by Gnomet (not verified) on Wed, 09/15/2010 - 18:17.

Thanks for the post! I tried similar configuration, but for some reason I was getting all the items tagged with any tag when searching with string that matched to some tag. After some testing, I found out that the below indexing worked like I was expecting, i.e. resulting only with those items that were tagged with the matching tag.

acts_as_taggable_on :tags
define_index do
  indexes taggings.tag.name, :as => :tags
end

I'm using thinking-sphinx (2.0.0.rc2) and acts-as-taggable-on (2.0.6)

Sara Trice's picture

Writing it as

Submitted by Sara Trice (not verified) on Fri, 05/20/2011 - 02:59.

Writing it as taggings.tag.name worked for me too. When I tried taggings.name it didn't pull any of the items with matching keywords. Thanks!

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>, <apache>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Drupal theme by Kiwi Themes.