Solr特殊字符转义处理

在Solr中,下列字符有特殊含义,需转义处理,否则查询下列字符会报查询错误。

+ – && || ! ( ) { } [ ] ^ ” ~ * ? : \ 


代码如下:

public static String transformSolrMetacharactor(String input){
        StringBuffer sb = new StringBuffer();
        String regex = "[+\\-&|!(){}\\[\\]^\"~*?:(\\)]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        while(matcher.find()){
            matcher.appendReplacement(sb, "\\\\"+matcher.group());
        }
        matcher.appendTail(sb);
        return sb.toString(); 
    }



你可能感兴趣的:(Solr,Solr4.1,特殊字符)