title: Scripted field not working in kibana
body: <pre><code>Elastic search version : 2.4.x and Kibana is 4.x
  </code></pre>
  
  <p>Data in elasticsearch: </p>
  
  <pre><code> {
              "_index": "testindex_201707",
              "_type": "abcd",
              "_id": "AV0rSOhWGrdL3plaGRY0",
              "_score": 1,
              "_source": {
                 "logtype": "xyz",
                 "filesize": 106390550,
                 "@timestamp": "2017-07-10T12:26:30.279+0530"
              }
           },
           {
              "_index": "testindex_201707",
              "_type": "xyz",
              "_id": "AV02YwV3GrdL3plaGRaD",
              "_score": 1,
              "_source": {
                 "bytes_read": 173,
                 "@timestamp": "2017-07-12T16:10:53.160+0530",
                 "logtype": "xyz",
                 "destination_port": "80"
              }
           }
  </code></pre>
  
  <p>I want to calculate the total HTTP/HTTPs traffic served for abcd and xyz. For this I have written the following Elasticsearch query:</p>
  
  <pre><code>GET /isp_vodafone_cdncache_201707/_search
  {
     "query": {
        "match_all": {}
     },
     "aggs": {
        "total_bytes_served": {
           "scripted_metric": {
              "init_script": "_agg[\"tempArray\"] = [];",
              "map_script": "if ((doc.logtype.value == \"abcd\")&amp;&amp;(doc.http_status_code.value == \"200\" || doc.http_status_code.value == \"200 OK\")) { _agg.tempArray.add(doc.filesize.value);} else if ((doc.logtype.value == \"xyz\")&amp;&amp;(doc.destination_port.value == \"80\"||doc.destination_port.value == \"443\")&amp;&amp;(doc.http_status_code.value == \"200\" || doc.http_status_code.value == \"200 OK\")) { _agg.tempArray.add(doc.bytes_read.value);}",
              "combine_script": "served = 0; for (i in _agg.tempArray) { served += i }; return served;",
              "reduce_script": "served = 0; for (j in _aggs) { served += j }; return served;"
           }
        }
     }
  }
  </code></pre>
  
  <p>This gives me <code>total_bytes_read</code> correctly. I want to display a metric in Kibana for Total HTTP/HTTPs traffic served. I tried to write a scripted field <code>total_bytes_served</code> in Kibana.</p>
  
  <pre><code>if (doc['logtype'].value == 'abcd'){ 
    return doc['filesize'].value;
  } else if ((doc['logtype'].value == 'xyz') AND 
             (doc['destination_port'].value == "80" OR 
              doc['destination_port'].value == "443")) {     
    return doc['bytes_read'].value;
  }
  </code></pre>
  
  <p>Using metrics aggregator, I selected SUM and the above-scripted field, but the metric value is blank.</p>
  
  <p>I have also tried enabling the following parameters in <code>elasticsearch.yml</code>:</p>
  
  <pre><code> script.engine.groovy.inline.aggs: true
   script.engine.painless.inline: true
   script.engine.expression.inline: true
  </code></pre>
  
  <p>Can somebody help me, what I am missing here?</p>