Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sunspot indexing and searching tags returns everything

I am using act_as_taggable_on for tagging in our projects, along with sunspot/solr for searching.

We get a strange unexpected result. First our setup (short version):

Model:

Class Person
  has_many :projects

  searchable do
    string :project_tags, :multiple => true do
      projects.map { |p| p.tag_list}.flatten
    end
  end

Taglist is a method from act_as_taggable_on which returns an array of tags for each projects (f.e. ["foo", "bar"]). We index the project tags for the project members.

When, in our controller, we do:

Person.search() do
   with(:project_tags).any_of(params[:tags])
end

This returns the right people. So far so good.

The Problem
We want to be able to search for multiple tags. So, per sunspot instructions, we pass along an array. The code looks roughly like this:

@tags_array= params[:tags].split(/ /)
Person.search() do
   with(:project_tags).any_of(@tags_array)
end

Now Sunspot gives us every person as a result, no matter what tags we use! We have been testing this in the console like crazy, but can't understand where we are going wrong.

Any help would be appreciated!
Erwin

like image 353
ErwinM Avatar asked Feb 20 '11 22:02

ErwinM


1 Answers

Ok we "solved" this ourselves and i'll report it back here in case anyone comes looking with the same question.

Somehow Sunspot doesn't like @tags_array in our search declaration, after some testing any @variable will not work. As soon as we changed it to:

tags_array= params[:tags].split(/ /)
Person.search() do
   with(:project_tags).any_of(tags_array)
end

it worked.

Cheers,
Erwin

like image 109
ErwinM Avatar answered Oct 01 '22 17:10

ErwinM