# ElasticSearch 5.5 Variable is not defined on using doc\['fieldname\]

**URL:** <https://discuss.elastic.co/t/elasticsearch-5-5-variable-is-not-defined-on-using-doc-fieldname/98478>\
**Category:** Elasticsearch\
**Created:** [August 27, 2017, 2:36pm UTC](https://discuss.elastic.co/t/elasticsearch-5-5-variable-is-not-defined-on-using-doc-fieldname/98478 "2017-08-27T14:36:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Faizan\_Sofi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/faizan_sofi/32/21166_2.png) [@Faizan\_Sofi](https://discuss.elastic.co/u/Faizan_Sofi)\
**Post date:** [August 27, 2017, 2:36pm UTC](https://discuss.elastic.co/t/elasticsearch-5-5-variable-is-not-defined-on-using-doc-fieldname/98478/1 "2017-08-27T14:36:59Z")

</div>

On using this agg query i get expected results  
curl -XPOST 'localhost:9200/.../.../\_search?pretty' -H 'Content-Type: application/json' -d'  
{  
"aggregations": {

```
                    "IMPRESSIONS": {
                        "sum": {
                            "script": 
                            {
                                "inline" : "if(doc[params.vc].empty){params.zero} else {doc[params.vc].value}",
                                "params" :{
                                "zero" : 0,
                                "vc" : "viewCount"
                                }

                            }

                        }
                    },
                    "VIEWABLE_IMPRESSIONS": {
                        "sum": {
                            "script": 
                            {
                                "inline" : "if(doc[params.v].empty){params.zero} else {doc[params.v].value}",
                                "params" :{
                                "zero" : 0,
                                "v" : "viewability"
                                }
                            }

                        }
                    }
                }

```

}'

but when using

curl -XPOST 'localhost:9200/.../.../\_search?pretty' -H 'Content-Type: application/json' -d'  
{  
"aggregations": {

```
                    "IMPRESSIONS": {
                        "sum": {
                            "script":{
                            "lang" : "painless",
                            "inline": "if(doc.containsKey('viewCount')){doc['viewCount'].value} else {0}"
                            }

                        }
                    },
                    "VIEWABLE_IMPRESSIONS": {
                        "sum": {
                           "script":{
                            "lang" : "painless",
                            "inline": "if(doc.containsKey('viewability')){doc['viewability'].value} else {0}"
                            }

                        }
                    }
                }

```

}'

where i am just using doc['fieldname']  
I get

"type" : "search\_phase\_execution\_exception",  
"reason" : "all shards failed",  
"phase" : "query",  
"grouped" : true,  
"failed\_shards" : [  
{  
"shard" : 0,  
"index" : "i...",  
"node" : "...",  
"reason" : {  
"type" : "script\_exception",  
"reason" : "compile error",  
"script\_stack" : [  
"if(doc.containsKey(viewCount)){doc[to ...",  
" ^---- HERE"  
],  
"script" : "if(doc.containsKey(viewCount)){doc[viewCount].value} else {0}",  
"lang" : "painless",  
"caused\_by" : {  
"type" : "illegal\_argument\_exception",  
"reason" : "Variable [totalOrderedItem] is not defined."  
}  
}  
}  
]  
},  
"status" : 500

---

<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:** [September 24, 2017, 2:37pm UTC](https://discuss.elastic.co/t/elasticsearch-5-5-variable-is-not-defined-on-using-doc-fieldname/98478/2 "2017-09-24T14:37:00Z")

</div>

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