title: Regular Expressions and Elastic Search
body: <p>I am trying to retrieve some company results using elasticsearch. I want to get companies that start with "A", then "B", etc. If I just do a pretty typical query with "prefix" like so</p>
  
  <pre><code>GET apple/company/_search
  {
    "query": {
      "prefix": {
        "name": "a"
      }
    },
    "fields": [
      "id",
      "name",
      "websiteUrl"
    ],
    "size": 100
  }
  </code></pre>
  
  <p>But this will return Acme as well as Lemur and Associates, so I need to distinguish between A at the beginning of the whole name versus just A at the beginning of a word.</p>
  
  <p>It would seem like regular expressions would come to the rescue here, but elastic search just ignores whatever I try. In tests with other applications, <code>^[\S]a*</code> should get you anything that starts with <code>A</code> that doesn't have a space in front of it. Elastic search returns 0 results with the following:</p>
  
  <pre><code>GET apple/company/_search
  {
    "query": {
      "regexp": {
        "name": "^[\S]a*"
      }
    },
    "fields": [
      "id",
      "name",
      "websiteUrl"
    ],
    "size": 100
  }
  </code></pre>
  
  <p>In FACT, the Sense UI for Elasticsearch will immediately alert you to a "Bad String Syntax Error". That's because even in a query elastic search wants some characters escaped. Nonetheless <code>^[\\S]a*</code> doesn't work either.</p>