solr sanitizing query

I am using solr with ruby on rails.
It’s all working well, I just need to know if there’s any existing code to sanitize user input, like a query starting with ? or *

  • Error When trying to load win32/sound with Ruby
  • ruby floating point errors
  • Alternate for action caching for normal gem ruby method?
  • Rails capistrano deployment failure
  • Repackaging a Ruby Gem
  • runs fine but ruby, irb, rails command “not found” (UB12.04)
  • Iterate over an Excel workbook and index everything?
  • Why is an ajax request not being triggered here?
  • How to sanitize form params for use with Searchlogic?
  • GitLab CI failing due to gem load error
  • Cannot find octokit gem when running ruby, but irb can find it
  • Rails console can't open due to “Please install the postgresql adapter” error, in newly created app that doesn't use postgres
  • 3 Solutions collect form web for “solr sanitizing query”

    I don’t know any code that does this, but theoretically it could be done by looking at the parsing code in Lucene and searching for throw new ParseException (only 16 matches!).

    In practice, I think you’re better off just catching any solr exceptions in your code and showing an “invalid query” message or something like that.

    EDIT: Here are a couple of “sanitizers”:

    The Solr Security and the Solr Query Syntax wiki pages may be relevant.

    If you are using Solarium with PHP then you can use the Solarium_Escape::term() method.

     * Escape a term
     * A term is a single word.
     * All characters that have a special meaning in a Solr query are escaped.
     * If you want to use the input as a phrase please use the {@link phrase()}
     * method, because a phrase requires much less escaping.\
     * @link
     * @param string $input
     * @return string
    static public function term($input)
        $pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
        return preg_replace($pattern, '\\\$1', $input);
    Ruby is the best programming language in the world - Ruby on Rails.