Description:
I'm experiencing an unexpected floating-point precision issue when performing aggregations on a scaled_float
field in Elasticsearch. I have defined a field with scaled_float
type and a scaling_factor
of 100. Here are the details:
Elasticsearch Version:
7.14.2
Index Mapping:
{
"mappings": {
"properties": {
"my_field": {
"type": "scaled_float",
"scaling_factor": 100
}
}
}
}
Sample Data:
POST /my_index/_doc/1
{
"my_field": 0.1
}
POST /my_index/_doc/2
{
"my_field": 0.2
}
Query: I executed the following aggregation to sum the values of my_field:
{
"aggs": {
"total": {
"sum": {
"field": "my_field"
}
}
}
}
Expected Result:
I expected the sum to be 0.3.
Actual Result:
The result returned was 0.30000000000000004.
Additional Information:
This result appears to be a floating-point precision issue, which is common in computer systems.
I would like to understand why this is happening and if there are any recommended approaches to handle floating-point precision in Elasticsearch.
Questions:
Is there a known issue with floating-point precision when using scaled_float fields?
What are the best practices for managing floating-point precision when performing aggregations in Elasticsearch?
Are there any solutions to round the result to a specific precision?
Thank you for your assistance!