Nemo

Nemo 关注TA

路漫漫其修远兮,吾将上下而求索。

Nemo

Nemo

关注TA

路漫漫其修远兮,吾将上下而求索。

  • 加入社区3,280天
  • 写了1,496,113字

该文章投稿至Nemo社区   Java  板块 复制链接


Luncene 特殊字符查询处理

发布于 2017/10/20 18:36 2,755浏览 0回复 819

参考solr的处理方式:

  /**
   * See: {@link org.apache.lucene.queryparser.classic queryparser syntax} 
   * for more information on Escaping Special Characters
   */
  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();
  }
本文标签
 {{tag}}
点了个评