在elasticsearch中转义特殊字符

问题描述

我正在使用 elasticsearch python 客户端 对 elasticsearch 实例进行一些查询我们正在托管.

I am using the elasticsearch python client to make some queries to the elasticsearch instance that we are hosting.

我注意到有些字符需要转义.具体来说,这些...

I noticed that some characters need to be escaped. Specifically, these...

+ - && || ! ( ) { } [ ] ^ " ~ * ? : 

除了我已经想到的之外,有没有一种干净的方法可以做到这一点?当然有比做更清洁的方法

Is there a clean way to do this beyond what I've already got in mind? Surely there is a cleaner way than doing

term
    .replace("+", "+")
    .replace("-", "-")

    # ....etc

我希望有一个我可以使用的 API 调用,但我在文档中找不到.这似乎是一个很常见的问题,应该由某人解决.

I was hoping there was an API call that I could use, but I can't find one in the docs. This seems like a common enough issue that it should have been solved by someone.

有人知道正确"的做法吗?

Does anyone know the "correct" way of doing this?

我仍然不确定是否有 API 调用,但我得到的东西足够简洁,足以让我满意.

EDIT : I am still not sure if there is an API call, but I got things concise enough to where I am happy.

def needs_escaping(character):                                                                                                                                                                                        

    escape_chars = {                                                                                                                                                                                               
        '\' : True, '+' : True, '-' : True, '!' : True,                                                                                                                                                           
        '(' : True, ')' : True, ':' : True, '^' : True,                                                                                                                                                            
        '[' : True, ']': True, '"' : True, '{' : True,                                                                                                                                                            
        '}' : True, '~' : True, '*' : True, '?' : True,                                                                                                                                                            
        '|' : True, '&' : True, '/' : True                                                                                                                                                                         
    }                                                                                                                                                                                                              
    return escape_chars.get(character, False)   


sanitized = ''
for character in query:                                                                                                                                                                                            

    if needs_escaping(character):                                                                                                                                                                                 
        sanitized += '\%s' % character                                                                                                                                                                           
    else:                                                                                                                                                                                                      
        sanitized += character 


解决方案

是的,您需要在 query_string 查询.为此(假设您使用的是 PyLucene),您应该能够使用 QueryParserBase.escape(String).

Yes, those characters will need to be replaced within content you want to search in a query_string query. To do that (assuming you are using PyLucene), you should be able to use QueryParserBase.escape(String).

除此之外,您始终可以根据您的需要调整 QueryParserBase.escape 源代码:

Barring that, you could always adapt the QueryParserBase.escape source code to your needs:

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();
}

相关文章