title: How to perform the arthimatic operation on data from elasticsearch
body: <p>I need to have average of cpuload on specific nodetype. For example if I give nodetype as <code>tpt</code> it should give the average of <code>cpuload</code> of <code>nodetype</code>'s of all <code>tpt</code> available. I tried different methods but vain...</p>
  
  <p>My data in elasticsearch is below:   </p>
  
  <pre><code>{
     "took" : 5,
     "timed_out" : false,
     "_shards" : {
       "total" : 5,
       "successful" : 5,
       "failed" : 0
     },
     "hits" : {
       "total" : 4,
       "max_score" : 1.0,
       "hits" : [
         {
           "_index" : "kpi",
           "_type" : "kpi",
           "_id" : "\u0003",
           "_score" : 1.0,
           "_source" : {
             "kpi" : {
               "CpuAverageLoad" : 13,
               "NodeId" : "kishan",
               "NodeType" : "Tpt",
               "State" : "online",
               "Static_limit" : 0
             }
           }
         },
         {
           "_index" : "kpi",
           "_type" : "kpi",
           "_id" : "\u0005",
           "_score" : 1.0,
           "_source" : {
             "kpi" : {
               "CpuAverageLoad" : 15,
               "NodeId" : "kishan1",
               "NodeType" : "tpt",
               "State" : "online",
               "Static_limit" : 0
             }
           }
         },
         {
           "_index" : "kpi",
           "_type" : "kpi",
           "_id" : "\u0004",
           "_score" : 1.0,
           "_source" : {
             "kpi" : {
               "MaxLbCapacity" : "700000",
               "NodeId" : "kishan2",
               "NodeType" : "bang",
               "OnlineCSCF" : [
                 "001",
                 "002"
               ],
               "State" : "Online",
               "TdbGroup" : 1,
               "TdGroup" : 0
             }
           }
         },
         {
           "_index" : "kpi",
           "_type" : "kpi",
           "_id" : "\u0002",
           "_score" : 1.0,
           "_source" : {
             "kpi" : {
               "MaxLbCapacity" : "700000",
               "NodeId" : "kishan3",
               "NodeType" : "bang",
               "OnlineCSCF" : [
                 "001",
                 "002"
               ],
               "State" : "Online",
               "TdLGroup" : 1,
               "TGroup" : 0
             }
           }
         }
       ]
     }
   }
  </code></pre>
  
  <p>And my query is </p>
  
  <pre><code>curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
  {
      "query": {
          "bool" : {
              "must" : {
                  "script" : {
                      "script" : {
                          "source" : "kpi[CpuAverageLoad].value &gt; params.param1",
                          "lang"   : "painless",
                          "params" : {
                              "param1" : 5
                          }
                      }
                  }
              }
          }
      }
  }'
  </code></pre>
  
  <p>but is falling as it is unable to find the exact source.</p>
  
  <pre><code>{
    "error" : {
      "root_cause" : [
        {
          "type" : "illegal_argument_exception",
          "reason" : "[script] unknown field [source], parser not found"
        }
      ],
      "type" : "illegal_argument_exception",
      "reason" : "[script] unknown field [source], parser not found"
    },
    "status" : 400
  }
  </code></pre>