# Why is this scripted metric aggregation query not working?

**URL:** <https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048>\
**Category:** Elasticsearch\
**Created:** [April 30, 2018, 4:39pm UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048 "2018-04-30T16:39:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticuser1](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@elasticuser1](https://discuss.elastic.co/u/elasticuser1)\
**Post date:** [April 30, 2018, 4:39pm UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048/1 "2018-04-30T16:39:28Z")

</div>

> PUT /ledger/\_doc/1  
> {  
> "type":"sale"  
> }
> 
> PUT /ledgeramt/\_doc/1  
> {  
> "amount":1000  
> }
> 
> POST /ledger,ledgeramt/\_search?size=0  
> {  
> "query" : {  
> "match\_all" : {}  
> },  
> "aggs": {  
> "profit": {  
> "scripted\_metric": {  
> "init\_script" : "params.\_agg.transactions = ",  
> "map\_script" : "params.\_agg.transactions.add(doc['type'].value == \u0027sale\u0027 ? doc['amount'].value : -1 \* doc['amount'].value)",  
> "combine\_script" : "double profit = 0; for (t in params.\_agg.transactions) { profit += t } return profit",  
> "reduce\_script" : "double profit = 0; for (a in params.\_aggs) { profit += a } return profit"  
> }  
> }  
> }  
> }

Error:

> {  
> "took": 10,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 10,  
> "successful": 8,  
> "skipped": 0,  
> "failed": 2,  
> "failures": [  
> {  
> "shard": 3,  
> "index": "ledger",  
> "node": "6nKisyggQtmWdv\_NVXPXjQ",  
> "reason": {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",  
> "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",  
> "params.\_agg.transactions.add(doc['type'].value == 'sale' ? doc['amount'].value : -1 \* doc['amount'].value)",  
> " ^---- HERE"  
> ],  
> "script": "params.\_agg.transactions.add(doc['type'].value == 'sale' ? doc['amount'].value : -1 \* doc['amount'].value)",  
> "lang": "painless",  
> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "No field found for [amount] in mapping with types "  
> }  
> }  
> }  
> ]  
> },  
> "hits": {  
> "total": 0,  
> "max\_score": 0,  
> "hits":   
> },  
> "aggregations": {  
> "profit": {  
> "value": 0  
> }  
> }  
> }

Is this not allowed? I am on ELK 6.2.1

Thanks.

---

<div class="post-metadata">

**Author:** ![elasticuser1](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@elasticuser1](https://discuss.elastic.co/u/elasticuser1)\
**Post date:** [May 1, 2018, 4:54pm UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048/2 "2018-05-01T16:54:57Z")

</div>

Anybody there? Please suggest. Thanks

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 2, 2018, 6:30am UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048/3 "2018-05-02T06:30:48Z")

</div>

The error is exactly what is stated in the response. The `amount` field does not exist in your index:

```auto
"reason": "No field found for [amount] in mapping with types []"

```

You should double check the rest of your script as well, though. For example, there is not `in` operator in painless, and variables must have a type, even if that type is `def`. I think mean to use the enhanced for loop, which uses colon (`:`).

```auto
for (def t : params._agg.transactions)

```

---

<div class="post-metadata">

**Author:** ![elasticuser1](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@elasticuser1](https://discuss.elastic.co/u/elasticuser1)\
**Post date:** [May 2, 2018, 8:22pm UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048/4 "2018-05-02T20:22:14Z")

</div>

Thanks @rjernst. But that's not the issue. I still get the same error. Here I am reading from 2 indexes.. How to create script for that? Thanks

POST /ledger,amt/\_search?size=0  
{  
"query" : {  
"match\_all" : {}  
},  
"aggs": {  
"profit": {  
"scripted\_metric": {  
"init\_script" : "params.\_agg.transactions = []",  
"map\_script" : "params.\_agg.transactions.add(doc['typ'].value == \u0027sale\u0027 ? doc['amount'].value : -1 \* doc['amount'].value)",  
"combine\_script" : "double profit = 0; for (def t: params.\_agg.transactions) { profit += t } return profit",  
"reduce\_script" : "double profit = 0; for (def a: params.\_aggs) { profit += a } return profit"  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 2, 2018, 9:48pm UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048/5 "2018-05-02T21:48:14Z")

</div>

The mapping must exist in both indexes, or you need to make your script work when the field does not exist. You can check for the mapping existing by doing `doc.containsKey('amount')`.

---

<div class="post-metadata">

**Author:** ![elasticuser1](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@elasticuser1](https://discuss.elastic.co/u/elasticuser1)\
**Post date:** [May 2, 2018, 10:53pm UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048/6 "2018-05-02T22:53:53Z")

</div>

It worked. Thanks @rjernst.

---

<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:** [May 30, 2018, 10:53pm UTC](https://discuss.elastic.co/t/why-is-this-scripted-metric-aggregation-query-not-working/130048/7 "2018-05-30T22:53:53Z")

</div>

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