title: Sorting numbers in textfield in ElasticSearch
body: <p>I've a issue when sorting a field in ElasticSearch with text and numbers in the same field.</p>
  
  <p>My pattern is something like this: <em>"text/number/year/text"</em>.</p>
  
  <p>I'm trying sort by this, but I get something like this:</p>
  
  <pre><code>"hits": [
           {            
              "_source": {
                 "fields": {
                    "numbered": "text/1/year/text",
                    "numbered-number": "1"
                 }
              },
              "sort": [
                 "1"
              ]
           },
           {            
              "_source": {
                 "fields": {
                    "numbered": "text/10/year/text",
                    "numbered-number": "10"
                 }
              },
              "sort": [
                 "10"
              ]
           },
           {            
              "_source": {
                 "fields": {
                    "numbered": "text/11/year/text",
                    "numbered-number": "11"
                 }
              },
              "sort": [
                 "11"
              ]
           },
           ...
            {            
              "_source": {
                 "fields": {
                    "numbered": "text/19/year/text",
                    "numbered-number": "19"
                 }
              },
              "sort": [
                 "19"
              ]
           },
           {            
              "_source": {
                 "fields": {
                    "numbered": "text/2/year/text",
                    "numbered-number": "2"
                 }
              },
              "sort": [
                 "2"
              ]
           },
  </code></pre>
  
  <p>Well, ElasticSearch are sorting this 1, 10, 11, 12, ..., 19, 2, 20, 21... How can I resolve this issue? I just need Natural Sort.</p>
  
  <h2>UPDATED:</h2>
  
  <p>I try this script, but it's not working too.</p>
  
  <pre><code>POST myindex/_search
  {
     "from": 0,
     "size": 40,
     "sort": [
        {
           "_script": {
              "type": "string",
              "script": {
                 "inline": 
                 "if ('fields.myfield.sort' =~ /\\d+/) { return Integer.parseInt(doc['fields.myfield.sort'].value); }"               
              },
              "order" : "asc"
  
           }
        }
     ],
      "_source": { "include": ["fields.myfield"] }
  }
  </code></pre>
  
  <h2>Error</h2>
  
  <pre><code>"reason": {
                 "type": "null_pointer_exception",
                 "reason": null
  </code></pre>