问题
遇到的异常信息
bash
: Lexical error at line 1, column 38. Encountered: <EOF> after
at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:114)
解决办法
查询搜索时遇到了特殊字符,需要对字符串进行转义
java
public static String escape(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
|| c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
|| c == '*' || c == '?' || c == '|' || c == '&' || c == '/') {
sb.append('\\');
}
sb.append(c);
}
return sb.toString();
}