Search code examples
mysqlsqlruby-on-railsrubythinking-sphinx

Thinking Sphinx - search title, content and tags of blog posts


I'm trying to update a search function for blog posts that searches the title, content, and tags. Right now, Thinking Sphinx is not searching through the tags. It's executing the following query:

SELECT * FROM `blog_post_core` WHERE MATCH('vacation')

How do I get the search to include the tags? Below is the index file.

ThinkingSphinx::Index.define 'blog/post', with: :active_record do
  indexes :title
  indexes :content
  indexes "replace(tag_cache, '#{TAG_SEPARATOR}', ' ')", as: :tag_name
  indexes user.username, as: :author
  has :created_at
end

Edit: The TAG_SEPARATOR is a string along the lines of "!!!". The tag_cache is a string containing all the tags, but separated, such as "vacation!!!work". Is the replace actually getting the :tag_cache attribute and splitting it into many tags?


Solution

  • Perhaps you're aware of this, but if not: the thing about Sphinx and how Thinking Sphinx integrates with it: inserts/updates aren't automatically added to your Sphinx index files.

    Thus, if you want your search results to be accurate, you need to run the ts:index rake task regularly.

    ts:rebuild, mentioned in the comments, stops Sphinx, then runs ts:index, then starts Sphinx up again - but if you're not changing the structure of your indices (just the data), then there's no need to have Sphinx stop/start. It's kinda like the equivalent of db:migrate. Best to stick to ts:index for scheduled tasks.