Scaled_float rounding oddly

I thought I understood scaled float, and up until today they have worked as expected, but I indexed some new data and scaled_float fields got rounded in a way I can't explain.

Consider the below mapping - taken direct from the index in question:

"daily": {
        "properties": {
          "cost": {
            "type": "scaled_float",
            "ignore_malformed": false,
            "scaling_factor": 6,
            "coerce": true
          },
          "cost_per_kwh": {
            "type": "scaled_float",
            "ignore_malformed": false,
            "scaling_factor": 4,
            "coerce": true
          },
          "kwh": {
            "type": "scaled_float",
            "ignore_malformed": false,
            "scaling_factor": 3,
            "coerce": true
          }
        }
      }

Now consider the below document found in it:

{
  "_source": {
    "daily": {
      "kwh": 10.197,
      "cost": 4.45,
      "cost_per_kwh": 0.436403
    },
  },
  "fields": {
    "daily.kwh": [
      10.333333333333334
    ],
    "daily.cost": [
      4.5
    ],
    "daily.cost_per_kwh": [
      0.5
    ]
  }

You can see the correct values in the _source - this is what I expect to saved as a scaled_float, yet all three numbers are being rounded oddly.

Take for example cost - it has a scaling factor of 6 so I'd expect 6 decimal points of precision to be saved - yet the source value of 4.45 has been stored as 4.5.
Then also consider kwh, which not only has a similar rounding error, but has 15 decimal points of precision despite having a scaling factor of 3!

I find this most perplexing- how can it be?

I found my problem. The scaled_float parameter isn't the number of figures after the decimal point, it's just the number to multiply the input value by - so if you want 6 digits of precision you need to use 10^6 or 1000000 in this field.

1 Like

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