views:

137

answers:

1

In our application we're using acts_as_solr for search. Everything seems to be running smoothly except for the fact that using the model name as the search query returns every single row in the table.

For example, let's say we have a users table. We specify acts_as_solr in our model to search the fields first name, last name and handle acts_as_solr :fields => [:handle, :lname, :fname]. When you use "user" as the search term it returns every single user in the system, or every row in the database as a result.

Has anyone else run into this?

+1  A: 

This is due to the way acts_as_solr builds the query within parser_methods.rb

There are few changes you will need to make within parser_methods.rb

Change

models =  "AND #{solr_configuration[:type_field]}:#self.name}" 

to

models =  "#{solr_configuration[:type_field]}:#self.name}" 

Then don't include the constructed models string into the query.

query = "(#{query.gsub(/ *: */,"_t:")}) #{models}"

Change this to just

query = "(#{query.gsub(/ *: */,"_t:")})"

Now you will need to force the models portion of this query into the search over to SOLR.

I did this with.....

query_options[:fq_model] = models

Went over to standard.rb

Made sure to add

:fq_model

into the VALID_PARAMS Array.

Added the following

if @params[:fq_model]
 hash["fq"] = @params[:fq_model]
end

I added some extra cases but you shouldn't need them if unless you want to make :fq an option on find_by_solr.

After I had these all in place I can now search for the model name and I get the much smaller result set I would expect.

Chances are that multi_solr_search will need updates to work as well since it appears that the type_t portion of the query is hard coded into that method.

Figured most of this out when I ran into some highlighting issues related to this question. http://stackoverflow.com/questions/2548750/highlighting-in-solr-1-4-requirefieldmatch

Mark Redding