Search code examples
elasticsearchelasticsearch-painless

Elasticsearch "Painless" returns wrong and rounded values


Running Elasticsearch 7.3.2. I have two float fields "timeEnd" and timeStart. I simply want to return difference.

In Kibana index-patterns I created a scripted variable "rate":

if (doc['timeEnd'].length==0) return null;
if (doc['timeStart'].length==0) return null;
return doc['timeEnd'].value-doc['timeStart'].value;

This is what it returns:

[
 {
  "timeEnd": null,
  "timeStart": 1571760869.518571,
  "rate": [
   null
  ]
 },
 {
  "timeEnd": 1571760898.821922,
  "timeStart": 1571760736.872881,
  "rate": [
   128
  ]
 },
 {
  "timeEnd": 1571760893.483377,
  "timeStart": 1571760879.161604,
  "rate_mbps": [
   0
  ]
 },...

Most returned values are 0 or 128.


Solution

  • timeEnd and timeStart are of float type , which is a single-precision 32-bit floating point number. You need to change them to double which is a double-precision 64-bit floating point number