转https://blog.csdn.net/lich0000/article/details/8682814
https://blog.csdn.net/wgw335363240/article/details/39889979
在Solr中,下列字符有特殊含义,需转义处理,否则查询下列字符会报查询错误。
+ – && || ! ( ) { } [ ] ^ ” ~ * ? :
可直接调用
ClientUtils.escapeQueryChars("solr search")
源码如下
https://svn.apache.org/repos/asf/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/util/ClientUtils.java public static String escapeQueryChars(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); // These characters are part of the query syntax and must be escaped if (c == '\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' || c == '|' || c == '&' || c == ';' || c == '/' || Character.isWhitespace(c)) { sb.append('\'); } sb.append(c); } return sb.toString(); }