Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is scaled_float in Elasticsearch not rounding decimal places?

I wanted to try out the scaled_float from Elasticsearch but could not get my head around the following:

If I create a mapping for my index as in the documentation:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "price": {
          "type": "scaled_float",
          "scaling_factor": 100
        }
      }
    }
  }
}

And then I add some data to the Index like so:

PUT my_index/my_type/1
{
  "price" : 100.1599999999
}

I expect to get a price of 100.16 back, instead the result of GET my_index/my_type/1 shows me:

{
  "_index": "my_index",
  "_type": "my_type",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "price": 100.1599999999
  }
}

Why is it not rounded?

Or is the rounded value only used in the index and what I see here is the saved original input, because it is under "_source"? How can I check if the rounding is happening?

I am using Elasticsearch 5.6.5 with Lucene 6.6.1

Thanks in advance!

like image 293
Clem Niem Avatar asked Sep 04 '25 17:09

Clem Niem


1 Answers

Whatever you index in the _source will never change, however, underneath the price will be indexed as 100.16

If you run this query, you'll see 100.16 instead of 100.1599999999

GET my_index/_search
{
  "size": 0,
  "aggs": {
    "avg": {
      avg": {
        "field": "price"
      }
    }
  }
}
like image 64
Val Avatar answered Sep 07 '25 08:09

Val