wilsonlee wilsonlee - 3 months ago 24
Java Question

Elasticsearch , Max length of mapping type long

my mapping :

POST /packtwo-order-sku-log
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"baitu": {
"properties": {
"order_id":{
"type": "long"
},
....
}
}
}
}


when I search

"query": {"term" : {"order_id" : 10160815114820888}}


OR

"query": {"match" : {"order_id" : 10160815114820888}}


I got hits 0;
But when I change order_id to 10160815114820887 , I got hits.
However, the JSON ES returned shows :

"hits": [
{
"_index": "packtwo-order-sku-log",
"_type": "baitu",
"_id": "AVaMWcchVwJTsNV878q2",
"_score": 2.7917593,
"_source": {
"order_id": 10160815114820888,
...
}
}


I searched 10160815114820888 -> no result

I searched 10160815114820887 -> result is 10160815114820888

I find long type in official doc :

long

A signed 64-bit integer with a minimum value of -2^63 and a maximum value of 2^63-1


My data is not longer than 2^63-1

So,what is my problem?

Val Val
Answer

That's due to a rounding issue for IEEE-754 double-precision floating point values.

Whole values up until 53 bits can be represented safely, however, 10160815114820887 is 54 bits long (100100000110010011010100011111100011000001110100010111)

The real number you have indexed was indeed 10160815114820887, but due to the above-mentioned rounding issues, it was indexed and shows as 10160815114820888

You can try the following in your browser's Javascript console:

> var num = 10160815114820887;      <--- assign value
< undefined
> num                               <--- display value
< 10160815114820888

You can also try a quick test in your ES:

# create doc with 10160815114820887
POST test/test/1
{ "number": 10160815114820887 }

# get doc 1
GET test/test/1
# result
{ "number": 10160815114820888 }

As you can see, the number you have indexed (10160815114820887) shows up as 10160815114820888, and can be found as 10160815114820887 because it also gets rounded to 10160815114820888 at search time.