solr中特殊字符的處理

如查詢的條件爲:params.add("fq", "jiucaiyun_brand:Double A" ),這樣是查不出帶有空格的條件,須要把空格轉義app

if(brand2.contains(" ")) {
        brand2=brand2.replace(" " , "\\ ");
 }
 params.add("fq", "jiucaiyun_brand:" + brand2);這樣就能夠查出匹配的結果。.net

其餘字符處理:+ – && || ! ( ) { } [ ] ^ 」 ~ * ? : \和空格orm


public static String transformSolrMetacharactor(String input){
        StringBuffer sb = new StringBuffer();
        String regex = "[+\\-&|!(){}\\[\\]^\"~*?:(\\)\\s]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        while(matcher.find()){
            matcher.appendReplacement(sb, "\\\\"+matcher.group());
        }
        matcher.appendTail(sb);
        return sb.toString(); 
}
---------------------
做者:ldh_lyf
來源:CSDN
原文:https://blog.csdn.net/lyf_ldh/article/details/82775887
版權聲明:本文爲博主原創文章,轉載請附上博文連接!blog

相關文章
相關標籤/搜索