Search code examples
ruby-on-railsrubyruby-on-rails-4thinking-sphinxacts-as-taggable-on

Thinking-Sphinx with ActsAsTaggableOn tags search


I have a Rails app in which I use Thinking-Sphinx for search and ActsAsTaggableOn for tagging. I want to be able to include the currently selected tag in my search query. I have tried the following but not got it to work.

In my controller:

def show

  @team_tags = @team.ideas.tag_counts_on(:tags)

  if params[:tag]
    @ideas = @team.ideas.search(params[:search], :conditions => { tags: "tag" })
  else
    @ideas = @team.ideas.search(params[:search])
  end

end

My index for my Idea model:

ThinkingSphinx::Index.define :idea, :with => :real_time do
  [...]
  indexes tags.name, :as => :tags

  has user_id, type: :integer
  has team_id, type: :integer
  [...]
end

This gives me he following error:

ActionView::Template::Error (index idea_core: query error: no field 'tags' found in schema

When I have a tag selected my URLs looks like this:

/teams/1/tags/tag

So, what should I do to get Thinking-Sphinx and ActsAsTaggableOn to work together?


Solution

  • What you've got for your field will only work with SQL-backed indices, not real-time indices.

    In your case, what you want to do is create a method in your model that returns all the tag names as a single string:

    def tag_names
      tags.collect(&:name).join(' ')
    end
    

    And then you can refer to that in your index definition:

    indexes tag_names, :as => :tags
    

    Once you've done that, you'll need to regenerate your Sphinx indices, as you've changed the structure: rake ts:regenerate.