# How to perform the mathematical operation on data from elasticsearch

**URL:** https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051
**Category:** Elastic Training
**Created:** [March 1, 2018, 12:14pm UTC](https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051 "2018-03-01T12:14:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kish](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kish](https://discuss.elastic.co/u/kish)
#### Post date: [March 1, 2018, 12:14pm UTC](https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051/1 "2018-03-01T12:14:39Z")

</div>

I need to have average of cpuload on specific nodetype. My data in elastic search is below. i.e if i give nodetype as 'tpt' it should give the average of cpuload of nodetype's of all tpt available. I tried different methods but vain...

```
{
  "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
          }
        }
      }
    ]
  }
}
```

---

<div class="post-metadata">

### Author: ![kish](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kish](https://discuss.elastic.co/u/kish)
#### Post date: [March 2, 2018, 4:27am UTC](https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051/2 "2018-03-02T04:27:48Z")

</div>

And my query is

```
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "source" : "kpi[CpuAverageLoad].value > params.param1",
                        "lang" : "painless",
                        "params" : {
                            "param1" : 5
                        }
                    }
                }
            }
        }
    }
}'

```

but is falling as it is unable to find the exact source.

{  
"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  
}

---

<div class="post-metadata">

### Author: ![raposa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raposa/32/11906_2.png) [@raposa](https://discuss.elastic.co/u/raposa)
#### Post date: [March 2, 2018, 5:21am UTC](https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051/3 "2018-03-02T05:21:27Z")

</div>

Your Painless syntax for accessing the field is a bit off. Replace your `source` with the following and it should work:

```auto
"doc['kpi.CpuAverageLoad'].value > params.param1"

```

---

<div class="post-metadata">

### Author: ![kish](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kish](https://discuss.elastic.co/u/kish)
#### Post date: [March 2, 2018, 6:16am UTC](https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051/4 "2018-03-02T06:16:59Z")

</div>

Hi,  
Its still the same..  
Query:

> ```
> curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
> {
> "query": {
> "bool" : {
> "must" : {
> "script" : {
> "script" : {
> "lang": "expression",
> "source" : "doc['kpi.CpuAverageLoad'].value * 5",
> } } } } } }'
> 
> ```

Error:  
\> {

> ```
> "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
> }
> [
> 
> ```

---

<div class="post-metadata">

### Author: ![raposa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raposa/32/11906_2.png) [@raposa](https://discuss.elastic.co/u/raposa)
#### Post date: [March 2, 2018, 12:35pm UTC](https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051/5 "2018-03-02T12:35:13Z")

</div>

Ahhh...that was a different issue with your code. Make sure when you view the documentation that you select the version of the docs that matches the version of Elasticsearch you are using. The `source` parameter is for 6.x. Try changing `source` to `inline`:

```auto
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "inline" : "doc['kpi.CpuAverageLoad'].value > params.param1",
                        "lang" : "painless",
                        "params" : {
                            "param1" : 5
                        }
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 30, 2018, 12:35pm UTC](https://discuss.elastic.co/t/how-to-perform-the-mathematical-operation-on-data-from-elasticsearch/122051/6 "2018-03-30T12:35:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
