title: ElasticSearch sorting by regexp
body: <p>I have a field in ElasticSearch 6 index that can be matched with regexp. I need to sort search result so documents with values, that matches go before the ones, that don't. Is there some way to use regexp in sorting clause?</p>
  
  <p>Example document:</p>
  
  <pre><code> "mappings" : {
    "unit" : {
      "properties" : {
        "description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  </code></pre>
  
  <p>I thought about script sorting kind of this:</p>
  
  <pre><code>  "sort" : {
      "_script" : {
          "type" : "number",
          "script" : {
              "source": "regex('some_regexp_here').match(doc['description'].value) ? 1 : 0 ",
          },
          "order" : "desc"
        }
     }
  </code></pre>
  
  <p>Is it possible? Are there any other workarounds? Thank you.</p>