JSON input for heatmap Kibana 5.2.1

I am having filed "_type" with values 'call-end', 'call-attempt' and more. I want to divide count of 'call-attempt' with count of 'call-end' (failure/success) and display it in heatmap.

Here is my JSON input:

{"script": {
"lang": "painless",
"inline": "int totalSuccess = 0; int totalFailure = 0; for (int i = 0; i < doc['_type'].length; ++i) { if(doc['_type'][i] =='call-end') {totalSuccess += 1;} if(doc['_type'][i] == 'call-attempt'){totalFailure +=1;}} return totalFailure/totalSuccess ;"
}
}

If I return only 'totalFailure' or 'totalSuccess' it returns correct number. But if I want to divide it, it shows error - division by zero. But none of the value is zero.
What is the problem?

Hi Akaren,

Can you please show one of the documents so we can see the structure of what the script is working with?

In Dev Tools Console, just run a query such as:

GET my_index/my_type_search

And copy and paste a document that looks useful to help understand what the script is doing.

I don't have search I have this script inside visualization. How can I open it in Dev Tools?
This is my request. If I use aggregation "count" it ignores my JSON input field. So I use sum on field that is zero.

I have also tried scripted field that has only 1 and 0 and use the aggregation sum on it and just divide the result by for cycle to sum count of another value but it wasn't working either. When I used "_value" for result it showed nullpointer exception.

{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "type:call-attempt",
"analyze_wildcard": true
}
},
{
"range": {
"@timestamp": {
"gte": 1488872514925,
"lte": 1488919868771,
"format": "epoch_millis"
}
}
}
],
"must_not": []
}
},
"size": 0,
"_source": {
"excludes": []
},
"aggs": {
"2": {
"terms": {
"field": "attrs.dst_ca_name.keyword",
"size": 10,
"order": {
"1": "desc"
}
},
"aggs": {
"1": {
"sum": {
"script": {
"inline": "int totalSuccess = 0; int totalFailure = 0; for (int i = 0; i < doc['_type'].length; ++i) { if(doc['_type'][i] =='call-end') {totalSuccess += 1;} if(doc['_type'][i] == 'call-attempt'){totalFailure +=1;}} return totalFailure/totalSuccess ;",
"lang": "painless"
}
}
},
"3": {
"terms": {
"field": "attrs.src_ca_name.keyword",
"size": 5,
"order": {
"1": "desc"
}
},
"aggs": {
"1": {
"sum": {
"script": {
"inline": "int totalSuccess = 0; int totalFailure = 0; for (int i = 0; i < doc['_type'].length; ++i) { if(doc['_type'][i] =='call-end') {totalSuccess += 1;} if(doc['_type'][i] == 'call-attempt'){totalFailure +=1;}} return totalFailure/totalSuccess ;",
"lang": "painless"
}
}
}
}
}
}
}
}
}

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