Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sphinx - When to use 'has' and 'indexes' for fields

I installed Sphinx and Thinking-Sphinx some days ago on my ruby on rails 2.3.2, and basic searching works great. This means, without any conditions. Now, I want to filter the search with some conditions.

I have the Announcement model, and the index looks as follows:

  define_index do
    indexes title, :as => :title, :sortable => true
    indexes description, :as => :description, :sortable => true
  end

Maybe I'm wrong, but I noticed that only when I added the :sortable => true syntax to these attributes, I could use them as conditions in my search. Otherwise it won't find anything.

Now, I'm also using acts_as_taggable_on plugin, which generated my two db tables: tags and taggings. I don't have a model for tags, I just have acts_as_taggable_on :tags, :categories at my Announcements model.

What I'd like to do now is to filter by tags. So, I tried adding to my index has tags(:id), :as => :tag_ids with no luck, and also indexes tags(:id), :as => :tag_ids but it didn't work either.

How can I build the indexes so I can do something like this:

Announcement.search 'some word', :conditions => {:tag_ids => some_id}

And also, which is the different between has and indexes

Thanks, Brian

like image 500
Brian Roisentul Avatar asked Jan 16 '10 16:01

Brian Roisentul


1 Answers

Let me answer your questions in reverse. indexes whatever expects a string, this is what sphinx will search for the text you provide.

On the other hand, has whatever does NOT add this content to the searchable fields. It expects primarily numbers, because you use this stuff for sorting and filtering after sphinx has already done the search.

Finally, I believe you want has tags(:id), :as => :tag_ids in your model, and :with => {:tag_ids => some_id} in your search instead of using :conditions. Conditions are used on text fields that you have indexed, as a way to perform text searches on specific fields instead of all indexed fields. With is used to filter results using the attributes you've specified with has whatever.

like image 183
Jaime Bellmyer Avatar answered Oct 13 '22 20:10

Jaime Bellmyer