Why is the type scaled_float in Elasticsearch has "float" in its name?

Viewed 276

The documentation of Elasticsearch states

scaled_float A floating point number that is backed by a long, scaled by a fixed double scaling factor.

As long is an integer, it sounds to me like a fix-comma number, not a float.

Am I missing something? Is it the recommend type for amounts? (As floats are not to be used)

1 Answers

further down in the documentation, it says;

For floating-point types, it is often more efficient to store floating-point data into an integer using a scaling factor, which is what the scaled_float type does under the hood. For instance, a price field could be stored in a scaled_float with a scaling_factor of 100. All APIs would work as if the field was stored as a double, but under the hood Elasticsearch would be working with the number of cents, price*100, which is an integer. This is mostly helpful to save disk space since integers are way easier to compress than floating points. scaled_float is also fine to use in order to trade accuracy for disk space. For instance imagine that you are tracking cpu utilization as a number between 0 and 1. It usually does not matter much whether cpu utilization is 12.7% or 13%, so you could use a scaled_float with a scaling_factor of 100 in order to round cpu utilization to the closest percent in order to save space.

to expand on that example, a price of $1.10 and a scaling factor of 100 would mean the price is stored as 1.10 * 100, or 110, an integer. then when it's read back Elasticsearch will convert the value to it's original by doing the opposite - 110 / 100 to give 1.10.

also to nit pick, it's Elasticsearch, the s is not camel cased :)

Related