【solr】【JS】【java】处理solr特殊字符
1、js处理solr特殊字符
1 function solrTransformMetacharactor(data) { 2 if ("" == data) { 3 return data; 4 } 5 var str = ''; 6 var array = data.split(''); 7 var pattern = new RegExp("[+\\-&|!(){}\\[\\]^\"~*?:]"); 8 for (var i = 0, length = array.length; i < length; i++) { 9 if (pattern.test(array[i])) { 10 str += "\\" + array[i]; 11 } else { 12 str += array[i]; 13 } 14 } 15 return str; 16 }
2、java处理solr特殊字符
1 public static String solrTransformMetacharactor(String input) { 2 StringBuffer sb = new StringBuffer(); 3 String regex = "[+\\-&|!(){}\\[\\]^\"~*?:(\\)\\s]"; 4 Pattern pattern = Pattern.compile(regex); 5 Matcher matcher = pattern.matcher(input); 6 while (matcher.find()) { 7 matcher.appendReplacement(sb, "\\\\" + matcher.group()); 8 } 9 matcher.appendTail(sb); 10 return sb.toString(); 11 }