1

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?

wilsonlee
  • 15
  • 1
  • 5

1 Answers1

4

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.

Community
  • 1
  • 1
Val
  • 207,596
  • 13
  • 358
  • 360
  • Just as your ES example. you said "but can still be found as 10160815114820887",but I can't got the result when I search number 10160815114820887. I have to search 0888 if I want to get this data. So I think it's not only show problem but also change my data is ES – wilsonlee Aug 15 '16 at 09:54
  • A little confused about the last part. My result is , for example,I indexed 7 . When I search 7 ,I got nothing ,but when I search 8 , I got an answer of 8.So ,the problem is when I indexed the number,it get rounded to a wrong one. Right? – wilsonlee Aug 15 '16 at 10:09
  • 1
    Yes, that's correct. And the same goes at search time – Val Aug 15 '16 at 10:09
  • So the solution is to make the number short or save it as a String ? Is there a better way ? – wilsonlee Aug 15 '16 at 10:17
  • 1
    You can save them as string and it will work better, but you might run into other issues depending on what you need those numbers for (sorting, scripting, range queries, etc). – Val Aug 15 '16 at 10:17