Accessing payload in nested object with groovy

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
}
}
}
}
}]
}

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

Hi Alexander

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

Giuseppe

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

1 Like