0

In order to assign tags, users of my software search their existing tags (which is populated in an autocomplete). I want to index this search in Sphinx so the tag search is ultra responsive. I have seen posts on how to index a Model's tags with sphinx which helps when trying to find all the objects with that tag name, but I want to index the tag search itself for people searching for existing tags.

I am trying to use Sphinx for this search

Lead.tag_counts(:conditions => ["tags.name like ?", 'accounting%'])

Which produces the SQL

SELECT tags.id, tags.name, COUNT() AS count FROM tags LEFT OUTER JOIN taggings ON tags.id = taggings.tag_id AND taggings.context = 'tags' INNER JOIN leads ON leads.id = taggings.taggable_id WHERE ((taggings.taggable_type = 'Lead' AND tags.name like 'accounting%' )) GROUP BY tags.id, tags.name HAVING COUNT()

0

I assume I have to add indexes to the Tag model to allow the below to work, but not quite sure what they should be.

Tag.sphinx_leads_scope.search(params[:tag])

Thanks, John-Paul

Community
  • 1
  • 1
jnarowski
  • 113
  • 3

1 Answers1

0

Given acts-as-taggable-on has its own Tag class, this gets a little tricky, but if you put this code somewhere (perhaps in an initializer?), I think it should work - though it's untested:

ActsAsTaggableOn::Tag.define_index do
  indexes name
end

Adding a sphinx scope is trickier - you'd need to monkeypatch ActsAsTaggableOn::Tag for that. Definitely not out of the question, though. You also may want to add a filter for taggable type via taggings... although string filters are very limited - make sure you read the docs. Also, if you are going down the string filter path, you'll end up with possibly multiple tagging types for a tag, and so you'll want to concatenate the CRC32'd values with commas... check out this somewhat related answer (for the code snippets).

Community
  • 1
  • 1
pat
  • 16,116
  • 5
  • 40
  • 46