views:

107

answers:

1

Hi,

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

+3  A: 

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.

Jaime Bellmyer
Thank you very much! I'll try it.
Brian Roisentul
I don't know why, but it's not working. As I said before, I don't have a Tag model, I just use the acts_as_taggable_on plugin. Is it possible that issue is causing the problem?
Brian Roisentul
I mean, I don't have a field 'tags' at announcements table. The plugin generates two tables: tags and taggings. So, the relationship between the announcements and tags is through taggings table. Does it have something to do with this?
Brian Roisentul
Honestly, I'm not sure because I haven't used the acts_as_taggable_on plugin before. From what you posted above, I assumed that it added a tags instance method to your Announcment model, so you could say "announcement.tags" and get an array of the tags associated with the given announcement.After checking the plugin, this seems to be the case, so I'm at a loss for your issue.
Jaime Bellmyer
Ok. Thanks anyway. I'll create a new post with that issue then.
Brian Roisentul