Elasticsearch painless returns 错误的长值
Elasticsearch painless returns wrong long values
Elasticsearch returns 当我尝试从时间戳中获取毫秒时的舍入值(使用 docker.elastic.co/elasticsearch/elasticsearch:7.11.0 图像)。
我的映射:
{
"mappings": {
"properties": {
...
"updated_at": {
"type": "date"
}
...
}
}
}
我的脚本查询:
{
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "doc['updated_at'].value.millis"
}
}
}
}
结果:
{
...
"hits": {
...
"max_score": 1616185130000,
"hits": [
{
...
"_score": 1616185130000, <---- wrong value
"_source": {
...
"updated_at": 1616185148409 <---- should be this
}
},
{
...
"_score": 1616185130000, <---- same wrong value even if updated_at is different
"_source": {
...
"updated_at": 1616185148408 <---- should be this
}
}
...
还尝试在映射中使用 long 类型:
{
"mappings": {
"properties": {
...
"updated_at": {
"type": "long"
}
...
}
}
}
查询:
{
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "doc['updated_at'].value"
}
}
}
}
得到了相同的结果。
我无法使用排序,因为我们需要通过公式进行提升,而我遇到了这个问题。
原因是因为每个文档的_score
是type float
.
Java 中的 long
值具有 64 位精度,而 float
具有 32 位精度,因此尝试将 long 放入 float 会使您失去一些精度。
试试这个:
long test = 1616185148409L;
System.out.println("Value as long: " + value);
System.out.println("Value as float: " + (float)value);
结果:
Value as long: 1616185148409
Value as float: 1.61618513E12
这与您所看到的一致。
Elasticsearch returns 当我尝试从时间戳中获取毫秒时的舍入值(使用 docker.elastic.co/elasticsearch/elasticsearch:7.11.0 图像)。
我的映射:
{
"mappings": {
"properties": {
...
"updated_at": {
"type": "date"
}
...
}
}
}
我的脚本查询:
{
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "doc['updated_at'].value.millis"
}
}
}
}
结果:
{
...
"hits": {
...
"max_score": 1616185130000,
"hits": [
{
...
"_score": 1616185130000, <---- wrong value
"_source": {
...
"updated_at": 1616185148409 <---- should be this
}
},
{
...
"_score": 1616185130000, <---- same wrong value even if updated_at is different
"_source": {
...
"updated_at": 1616185148408 <---- should be this
}
}
...
还尝试在映射中使用 long 类型:
{
"mappings": {
"properties": {
...
"updated_at": {
"type": "long"
}
...
}
}
}
查询:
{
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "doc['updated_at'].value"
}
}
}
}
得到了相同的结果。
我无法使用排序,因为我们需要通过公式进行提升,而我遇到了这个问题。
原因是因为每个文档的_score
是type float
.
Java 中的 long
值具有 64 位精度,而 float
具有 32 位精度,因此尝试将 long 放入 float 会使您失去一些精度。
试试这个:
long test = 1616185148409L;
System.out.println("Value as long: " + value);
System.out.println("Value as float: " + (float)value);
结果:
Value as long: 1616185148409
Value as float: 1.61618513E12
这与您所看到的一致。