Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch float/double field type precision

I am experimenting with float/double fields in Elasticsearch (version 6.6.0). I have created this index:

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "amount": {
            "type" : "float"
        }
      }
    }
  }
}

and I added this entry:

PUT my_index/_doc/1
{
  "amount" : 1.0000000000111111111122222222223333333333
}

When I retrieve this document, I get the exact amount I have sent to Elastic:

GET my_index/_doc/1
...
    "_source": {
        "amount": 1.0000000000111111111122222222223333333333
    }
...

By checking the documentation, I would have expected the returning value to have less precision (less digits): Elastic numeric data types documentation

float - A single-precision 32-bit IEEE 754 floating point number, restricted to finite values.

I wonder if Elasticsearch is keeping instead the value as a BigDecimal (or something similar), but I could not find any reference to that.

My question: is this documented somewhere? Am I missing something here?

like image 589
botismarius Avatar asked Nov 15 '25 20:11

botismarius


1 Answers

Following V-K's comment, I have experimented more and I observed that:

  • Elasticsearch will return the original value sent to it (eg: 17.3), even if that value cannot be represented exactly in the type specified in the mapping (eg: integer);
  • the value converted to the type specified in the mapping (eg: integer) will be used when evaluating search queries. For example, requesting all document for which integer field > 17.1 will NOT return a document for which the integer field was set to 17.3 (because internally Elasticsearch will see field == 17).
like image 102
botismarius Avatar answered Nov 17 '25 11:11

botismarius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!