title: How can I use NEST QueryString and escape the special characters?
body: <p>I am using NEST to communicate with Elasticsearch in my applications.</p>
  
  <p>In this case, the user enters in their search term <code>F5503904902</code> which returns the correct result. However, if they search for the query <code>F5503904902-90190</code> or <code>F5503904902-90190_55F</code> the results do not come back.</p>
  
  <p>I assumed this was because of the special characters, so I attempted to escape them - but then no results come back either. Is my query correct, am I doing something wrong? <strong>Also</strong> I am appending a wildcard to the end of the escaped query to match anything open ended.</p>
  
  <p><strong>Search Method:</strong></p>
  
  <pre><code>public IPagedSearchResult&lt;MyFileObject&gt; Find(ISearchQuery query)
  {
      ElasticClient client = ElasticClientManager.GetClient(_indexCluster, ElasticSearchIndexName.MyFileObjects);
      string queryString = EscapeSearchQuery(query.Query) + "*"; 
      var searchResults = client.Search&lt;MyFileObject&gt;(s =&gt; s
          .From(query.Skip)
          .Size(query.Take)
          .QueryString(queryString));
  
  
  
      IPagedSearchResult&lt;MyFileObject&gt; pagedSearchResult = new PagedSearchResult&lt;MyFileObject&gt;();
      pagedSearchResult.Results = searchResults.Documents;
      pagedSearchResult.Skip = query.Skip;
      pagedSearchResult.Take = query.Take;
      pagedSearchResult.Total = Convert.ToInt32(searchResults.Total);
  
      return pagedSearchResult;
  }
  </code></pre>
  
  <p><strong>Escape Method:</strong></p>
  
  <pre><code>private string EscapeSearchQuery(string query)
  {
      if (String.IsNullOrWhiteSpace(query)) return query;
  
      //&amp;&amp; || not handled here
      char[] special = { '+', '-', '=', '&gt;', '&lt;', '!', '(', ')', '{', '}', '[', ']', '^', '\"', '~', '*', '?', ':', '\\', '/', ' ' };
      char[] qArray = query.ToCharArray();
  
      StringBuilder sb = new StringBuilder();
  
      foreach (var chr in qArray)
      {
          if (special.Contains(chr))
          {
              sb.Append(String.Format("\\{0}", chr));
          }
          else
          {
              sb.Append(chr);
          }
      }
  
      return sb.ToString();
  }
  </code></pre>
  
  <p>I would love any help or pointers why this isn't working or better ways to accomplish this.</p>