solr sanitizing query

前端 未结 2 2186
梦谈多话
梦谈多话 2020-12-15 13:52

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 *

2条回答
  •  被撕碎了的回忆
    2020-12-15 14:35

    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 http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping%20Special%20Characters
     *
     * @param string $input
     * @return string
     */
    static public function term($input)
    {
        $pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
    
        return preg_replace($pattern, '\\\$1', $input);
    }
    

提交回复
热议问题