Sanitizing Solr requests

July 17, 2009 Pivotal Labs

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

Biography

More Content by Pivotal Labs
Previous
Introducing ActiveHash, ActiveYaml and ActiveFile – easy readonly, file-based models
Introducing ActiveHash, ActiveYaml and ActiveFile – easy readonly, file-based models

ActiveHash is a simple base class that allows you to use a ruby hash as a readonly datasource for an Active...

Next
Come to "An Evening with Palm webOS" at Pivotal Labs
Come to "An Evening with Palm webOS" at Pivotal Labs

On Tuesday, July 21st, we're hosting "An evening with Palm's webOS" here at Pivotal Labs. Mitch Allen, Sof...

Enter curious. Exit smarter.

Register Now