Take the 2-minute tour ×

I have a sunspot/solr setup for fulltext searching model attributes. My QA just searched on " and +, both of which caused a 500 error:

Solr Response: orgapachelucenequeryParserParseException_Cannot_parse__Encountered_EOF_at_line_1_column_0_Was_expecting_one_of_____NOT______________________________QUOTED______TERM______PREFIXTERM______WILDTERM__________________NUMBER______TERM____________

How can I make these query strings safe? Is there a method in Sunspot to handle this?

share|improve this question
possible duplicate of solr sanitizing query –  Mauricio Scheffer Aug 15 '11 at 20:54
add comment

1 Answer

There is no method in Sunspot to filter those, because they are valid in certain kinds of Lucene queries. Sunspot is using the DisMax Query Parser by default, so you can read its documentation to learn more about those characters.

[DisMax] is designed to be support raw input strings provided by users with no special escaping. '+' and '-' characters are treated as "mandatory" and "prohibited" modifiers for the subsequent terms. Text wrapped in balanced quote characters '"' are treated as phrases […]

If you intend to never use those characters, you can filter them yourself from queries(the back slash is to escape minus sign).

Post.search do
  keywords params[:q].gsub(/[+\-"]/,'')

You may want to wrap that in a controller method, if you're invoking Sunspot's search method within a controller, or a model method if you're calling Sunspot's solr_search method from within your class's own custom search method.

share|improve this answer
add comment

Your Answer


