solr中特殊字符的处理
如查询的条件为:params.add("fq", "jiucaiyun_brand:Double A" ),这样是查不出带有空格的条件,需要把空格转义
if(brand2.contains(" ")) {
brand2=brand2.replace(" " , "\\ ");
}
params.add("fq", "jiucaiyun_brand:" + brand2);这样就可以查出匹配的结果。
其他字符处理:+ – && || ! ( ) { } [ ] ^ ” ~ * ? : \和空格
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
版权声明:本文为博主原创文章,转载请附上博文链接!