2009-07-15 78 views
5

我在軌道上使用紅寶石solr。 這一切都運行良好,我只需要知道是否有任何現有的代碼來消毒用戶輸入,就像查詢開始?或*solr消毒查詢

回答

7

我不知道有這樣的代碼,但理論上可以通過查看parsing code in Lucene並搜索throw new ParseException(只有16個匹配!)來完成。

實際上,我認爲你最好在代碼中捕獲任何solr異常並顯示「無效查詢」消息或類似的東西。

編輯:這裏有一對夫婦的 「消毒劑」 的:

1

如果您使用Solarium和PHP,那麼您可以使用Solarium_Escape::term()方法。

/** 
* 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); 
}