ElasticSearch 在无痛脚本中舍入浮点值
ElasticSearch rounds float value in painless script
我在使用 ElasticSearch 的脚本功能时注意到舍入问题。
我有以下映射:
{
"my-index": {
"mappings": {
"my-document": {
"properties": {
"id": {
"type": "long"
},
"foo": {
"type": "float"
}
}
}
}
}
}
和一堆文档索引。一份文档的 foo
字段值为 159944154.8644
。当我发送以下查询时:
{
"query": {
"bool": {
"must": [
{ "match": { "id": 42 } }
]
}
},
"_source": {
"includes": ["id","foo"]
},
"script_fields": {
"foo_scripted": {
"script": {
"lang": "painless",
"inline": "doc['foo'].value"
}
}
}
}
我得到以下舍入值作为响应:
{
"_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 :(
]
}
}
知道如何解决这个问题吗?提前致谢。
ES 版本:5.3.0
映射将字段类型设置为 "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。尝试将字段类型更改为 "double"
,这是一个双精度 64 位 IEEE 754 浮点数。
我在使用 ElasticSearch 的脚本功能时注意到舍入问题。 我有以下映射:
{
"my-index": {
"mappings": {
"my-document": {
"properties": {
"id": {
"type": "long"
},
"foo": {
"type": "float"
}
}
}
}
}
}
和一堆文档索引。一份文档的 foo
字段值为 159944154.8644
。当我发送以下查询时:
{
"query": {
"bool": {
"must": [
{ "match": { "id": 42 } }
]
}
},
"_source": {
"includes": ["id","foo"]
},
"script_fields": {
"foo_scripted": {
"script": {
"lang": "painless",
"inline": "doc['foo'].value"
}
}
}
}
我得到以下舍入值作为响应:
{
"_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 :(
]
}
}
知道如何解决这个问题吗?提前致谢。
ES 版本:5.3.0
映射将字段类型设置为 "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。尝试将字段类型更改为 "double"
,这是一个双精度 64 位 IEEE 754 浮点数。