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