If you’re accepting user input for Solr (which I expect most projects using it are), you’ve probably noticed that you need to sanitize what queries you pass to Solr. After reading a bunch of conflicting documentation and blog posts, I put together a simple little module to handle it for you. It should strip out everything that would cause Solr to throw an error on a query string. Let me know if it works for you or if I missed any corner cases!
module SolrStringSanitizer
ILLEGAL_SOLR_CHARACTERS_REGEXP = /+|-|!|(|)|{|}|[|]|^||"|~|*|?|:|;|&&|||/
def self.sanitize(string)
if string
string.gsub(ILLEGAL_SOLR_CHARACTERS_REGEXP,"")
end
end
end
About the Author