title: Escaping special characters in elasticsearch
body: <p>I am using the <a href="https://elasticsearch-py.readthedocs.io/en/master/" rel="noreferrer">elasticsearch python client</a> to make some queries to the elasticsearch instance that we are hosting.</p>
  
  <p>I noticed that <a href="http://lucene.apache.org/core/3_4_0/queryparsersyntax.html#Escaping%20Special%20Characters" rel="noreferrer">some characters need to be escaped</a>. Specifically, these...</p>
  
  <pre><code>+ - &amp;&amp; || ! ( ) { } [ ] ^ " ~ * ? : \
  </code></pre>
  
  <p>Is there a clean way to do this beyond what I've already got in mind? Surely there is a cleaner way than doing</p>
  
  <pre><code>term
      .replace("+", "\+")
      .replace("-", "\-")
  
      # ....etc
  </code></pre>
  
  <p>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.</p>
  
  <p>Does anyone know the "correct" way of doing this?</p>
  
  <p>EDIT : I am still not sure if there is an API call, but I got things concise enough to where I am happy.</p>
  
  <pre><code>def needs_escaping(character):                                                                                                                                                                                        
  
      escape_chars = {                                                                                                                                                                                               
          '\\' : True, '+' : True, '-' : True, '!' : True,                                                                                                                                                           
          '(' : True, ')' : True, ':' : True, '^' : True,                                                                                                                                                            
          '[' : True, ']': True, '\"' : True, '{' : True,                                                                                                                                                            
          '}' : True, '~' : True, '*' : True, '?' : True,                                                                                                                                                            
          '|' : True, '&amp;' : True, '/' : True                                                                                                                                                                         
      }                                                                                                                                                                                                              
      return escape_chars.get(character, False)   
  
  
  sanitized = ''
  for character in query:                                                                                                                                                                                            
  
      if needs_escaping(character):                                                                                                                                                                                 
          sanitized += '\\%s' % character                                                                                                                                                                           
      else:                                                                                                                                                                                                      
          sanitized += character 
  </code></pre>