欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

记录lucene使用问题

程序员文章站 2022-07-09 10:27:11
...

1.问题

线上根据商品名称查询商品报错,使用如下代码错误复现:

    public static void main(String[] args) throws ParseException {

        QueryParser parser = new QueryParser("tag", new IKAnalyzer(true,false));
        Query query = parser.parse("白菜/ab");
        System.out.println(query);
    }
Exception in thread "main" org.apache.lucene.queryparser.classic.ParseException: Cannot parse '白菜/ab': Lexical error at line 1, column 6.  Encountered: <EOF> after : "/ab"
at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:127)
at com.youyun.util.TokenizerTest.main(TokenizerTest.java:65)
Caused by: org.apache.lucene.queryparser.classic.TokenMgrError: Lexical error at line 1, column 6.  Encountered: <EOF> after : "/ab"
at org.apache.lucene.queryparser.classic.QueryParserTokenManager.getNextToken(QueryParserTokenManager.java:1133)
at org.apache.lucene.queryparser.classic.QueryParser.jj_scan_token(QueryParser.java:589)
at org.apache.lucene.queryparser.classic.QueryParser.jj_3R_2(QueryParser.java:472)
at org.apache.lucene.queryparser.classic.QueryParser.jj_3_1(QueryParser.java:479)
at org.apache.lucene.queryparser.classic.QueryParser.jj_2_1(QueryParser.java:465)
at org.apache.lucene.queryparser.classic.QueryParser.Clause(QueryParser.java:216)
at org.apache.lucene.queryparser.classic.QueryParser.Query(QueryParser.java:171)
at org.apache.lucene.queryparser.classic.QueryParser.TopLevelQuery(QueryParser.java:160)
at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:117)

2.原因及解决方法

查询参数中包含需要转义的字符,可以通过QueryParser的静态方法escape()进行转义,其代码如下:

  /**
   * Returns a String where those characters that QueryParser
   * expects to be escaped are escaped by a preceding <code>\</code>.
   */
  public static String escape(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 == '/') {
        sb.append('\\');
      }
      sb.append(c);
    }
    return sb.toString();
  }

发现我们的关键字中确实存在字符’/'需要转义,修改完成即可!

相关标签: 搜索