本文主要是介绍lucene搜索关键词错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题
遇到的异常信息
: Lexical error at line 1, column 38. Encountered: <EOF> after
at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:114)
解决办法
查询搜索时遇到了特殊字符,需要对字符串进行转义
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();}
这篇关于lucene搜索关键词错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!