Accessing payload in nested object with groovy

alerting

(Giuseppe Tricarico) #1

Hi all

I have a problem with inline script for condition in a watch.

The payload response is like the one below, and I have to calculate the condition on the ratio

KO.dco_count/hits.total

I wrote

"condition" : {
"script" : {
"inline" : "return ctx.payload.aggregations.2.buckets.KO.doc_count/ctx.payload.hits.total > threshold",
"params" : {
"threshold" : 0.1
}
}
}

but it gives me error on "ctx.payload.aggregations.2.buckets.KO.doc_count"


payload

{
"responses": [{
"took": 34,
"timed_out": false,
"_shards": {
"total": 20,
"successful": 20,
"failed": 0
},
"hits": {
"total": 5434,
"max_score": 0.0,
"hits": []
},
"aggregations": {
"2": {
"buckets": {
"OK": {
"doc_count": 5343
},
"KO": {
"doc_count": 91
}
}
}
}
}]
}


(Alexander Reelsen) #2

Hey,

can you try and change the name of the aggregation to be not a number? Looks as if the 2in ctx.payload.aggregations.2.buckets.KO.doc_count is recognized as an array element and not a string.

--Alex


(Giuseppe Tricarico) #3

Hi Alexander

The json does not depend on me, it depends on elasticsearch response.

Giuseppe


(Alexander Reelsen) #4

Hey,

Can you please paste your full watch. The name of the aggregation bucket is defined by you as part of the request, so you have full control over it. You can rename it from 2 to sth like status and try again.

--Alex


(system) #5