1
votes

I noticed rounding issue when using scripting feature of ElasticSearch. I've the following mapping:

{
  "my-index": {
    "mappings": {
      "my-document": {
        "properties": {
          "id": {
            "type": "long"
          },
          "foo": {
            "type": "float"
          }
        }
      }
    }
  }
}

and a bunch of documents indexed. One document has a value of 159944154.8644 for foo field. When I send the following query:

{
  "query": {
    "bool": {
      "must": [
        { "match": { "id": 42 } }
      ]
    }
  },
  "_source": {
    "includes": ["id","foo"]
  },
  "script_fields": {
    "foo_scripted": {
      "script": {
        "lang": "painless",
        "inline": "doc['foo'].value"
      }
    }
  }
}

I'm getting the following rounded value in response:

{
  "_index": "my-index",
  "_type": "my-document",
  "_id": "42",
  "_score": 1,
  "_source": {
    "foo": 1.599441548644E8, //= 159944154.8644 -> correct!
    "id": 42
  },
  "fields": {
    "foo_scripted": [
      1.5994416E8 //= 159944160.0 -> rounded :(
    ]
  }
}

Any idea how to to fix this? Thanks in advance.

ES version: 5.3.0

1

1 Answers

2
votes

The mapping sets the field type to "float", which is a single-precision 32-bit IEEE 754 floating point number. This floating point representation is capable of storing at most 9 significant decimal digits. Try changing the field type to "double", which is a double-precision 64-bit IEEE 754 floating point number.