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

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
The Great Ruby IDE Smackdown of '09
The Great Ruby IDE Smackdown of '09

In a recent thread on the East Bay Ruby Meetup list, several people chimed in with Ruby IDE suggestions. I...

SpringOne Platform 2019 Presentations

Watch Now