当我尝试从时间戳(使用docker.elastic.co/elasticsearch/elasticsearch:7.11.0图像)获取millis时,Elasticsearch返回舍入的值。
我的映射:
{
"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"
}
}
}
}也得到了同样的结果。
我不能使用排序,因为我们需要通过公式提升,而我坚持这个问题。
发布于 2021-03-22 21:26:25
原因是因为每个文档的_score都是type float。
在Java语言中,long值的精度是64位,而float的精度是32位,所以尝试将一个长整型值放入一个浮点数中将会丢失一些精度。
试试这个:
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这和你看到的是一致的。
https://stackoverflow.com/questions/66746630
复制相似问题