• <small id='AOqSJ'></small><noframes id='AOqSJ'>

    1. <i id='AOqSJ'><tr id='AOqSJ'><dt id='AOqSJ'><q id='AOqSJ'><span id='AOqSJ'><b id='AOqSJ'><form id='AOqSJ'><ins id='AOqSJ'></ins><ul id='AOqSJ'></ul><sub id='AOqSJ'></sub></form><legend id='AOqSJ'></legend><bdo id='AOqSJ'><pre id='AOqSJ'><center id='AOqSJ'></center></pre></bdo></b><th id='AOqSJ'></th></span></q></dt></tr></i><div id='AOqSJ'><tfoot id='AOqSJ'></tfoot><dl id='AOqSJ'><fieldset id='AOqSJ'></fieldset></dl></div>

    2. <tfoot id='AOqSJ'></tfoot>
        <bdo id='AOqSJ'></bdo><ul id='AOqSJ'></ul>

        <legend id='AOqSJ'><style id='AOqSJ'><dir id='AOqSJ'><q id='AOqSJ'></q></dir></style></legend>

        在elasticsearch中转义特殊字符

        时间:2023-10-09
          <bdo id='ZSX9X'></bdo><ul id='ZSX9X'></ul>

            <i id='ZSX9X'><tr id='ZSX9X'><dt id='ZSX9X'><q id='ZSX9X'><span id='ZSX9X'><b id='ZSX9X'><form id='ZSX9X'><ins id='ZSX9X'></ins><ul id='ZSX9X'></ul><sub id='ZSX9X'></sub></form><legend id='ZSX9X'></legend><bdo id='ZSX9X'><pre id='ZSX9X'><center id='ZSX9X'></center></pre></bdo></b><th id='ZSX9X'></th></span></q></dt></tr></i><div id='ZSX9X'><tfoot id='ZSX9X'></tfoot><dl id='ZSX9X'><fieldset id='ZSX9X'></fieldset></dl></div>

              • <tfoot id='ZSX9X'></tfoot>
              • <legend id='ZSX9X'><style id='ZSX9X'><dir id='ZSX9X'><q id='ZSX9X'></q></dir></style></legend>

                <small id='ZSX9X'></small><noframes id='ZSX9X'>

                  <tbody id='ZSX9X'></tbody>
                1. 本文介绍了在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();
                  }
                  

                  这篇关于在elasticsearch中转义特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:大型数据集的 TFIDF 下一篇:有纯 Python Lucene 吗?

                  相关文章

                  最新文章

                2. <legend id='cIn40'><style id='cIn40'><dir id='cIn40'><q id='cIn40'></q></dir></style></legend>

                  <small id='cIn40'></small><noframes id='cIn40'>

                3. <i id='cIn40'><tr id='cIn40'><dt id='cIn40'><q id='cIn40'><span id='cIn40'><b id='cIn40'><form id='cIn40'><ins id='cIn40'></ins><ul id='cIn40'></ul><sub id='cIn40'></sub></form><legend id='cIn40'></legend><bdo id='cIn40'><pre id='cIn40'><center id='cIn40'></center></pre></bdo></b><th id='cIn40'></th></span></q></dt></tr></i><div id='cIn40'><tfoot id='cIn40'></tfoot><dl id='cIn40'><fieldset id='cIn40'></fieldset></dl></div>
                    <bdo id='cIn40'></bdo><ul id='cIn40'></ul>

                    1. <tfoot id='cIn40'></tfoot>