Painless using comparing to a float


(Josh Speer) #1

I'm trying to divide 2 buckets, and check against a float to see if the value has decreased
This works

"condition": {
"script": {
  "inline": "return ((ctx.payload.aggregations.today.buckets.latest60.doc_count)/(ctx.payload.aggregations.today.buckets.previous24.doc_count) < 1)",
  "lang": "painless"
}
},

however when I try to test against a float it's not working

"condition": {
"script": {
  "inline": "return ((ctx.payload.aggregations.today.buckets.latest60.doc_count)/(ctx.payload.aggregations.today.buckets.previous24.doc_count) < .75F)",
  "lang": "painless"
}

},

Here is what the buckets are returning

"aggregations": {
      "today": {
        "buckets": {
          "latest60": {
            "doc_count": 54983
          },
          "previous24": {
            "doc_count": 54999
          }
        }
      }
    }

(Josh Speer) #2

I'm removed the fields all together to test... This always comes back as true

"condition": {
"script": {
  "inline": "int total=0;  total = ((54983/55000) * 100);  if (total < 75) {return true}",
  "lang": "painless"
}

},

total should equal 99.969 so the logic should return false.


(Josh Speer) #3

and I've simplified that to

"condition": {
"script": {
  "inline": "return (((54983/55000) * 100) < 75)",
  "lang": "painless"
}

}

Which also fails.


(Josh Speer) #4

moved to x-pack sub


(Alexander Reelsen) #5

Hey,

welcome to the intricacies of java based rounding. As you are dividing to integers, the return value is 0, which then is multiplied with 100.

Change your calculation to return (((54983.0/55000) * 100) < 75) and suddenly everything works as expected as floating point numbers are used for the calculation...

--Alex


(Josh Speer) #6

Thanks Alex, I was also given another solution that does the same, from support:

return ((((double)54983/55000) * 100) < 75)

but I appreciate the reply!


(system) #7

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