Math.round in painless script

I have to create a aggs like below:

'conversions_rate' => [
'avg' => [
'field' => 'conversions_rate',
'script' => [
'lang' => 'painless',
'inline' => 'Math.round(_value * params.ten)',
'params' => [
'ten' => 100
]
],
'missing' => 0
]
]

but the Math.round is not work any more. I still get a float.
How can I fix it ?

Not sure I understand, Math.round() requires a float/double as an argument, but returns a long. However the aggregation result will be casted to a float again, if that's what you mean. See this example, of avering two longs

DELETE test

PUT test/_doc/1
{
  "key" : 10
}

PUT test/_doc/2
{
  "key" : 20
}

GET test/_search
{
  "size": 0,
  "aggs": {
    "avg": {
      "avg": {
        "field": "key"
      }
    }
  }
}

Is this what you mean with still getting a float? If so, the float should still be the correct value.

Thanks a lot. That is what I mean.
In my mind I thought the Math.round() will return the integer like this (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round)
before read your reply.

Is this feature that return a float is due to Java's feature?

No, the reason for this is, that aggregation values are always returned as float. See https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-aggregations.html

I see. Thank you so much.

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