0

I used python ship data from mongodb to elasticsearch.

There is a timestamp field named update_time, mapping like:

    "update_time" : {
        "type": "date",
        "format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    },

epoch_millis is used for timestamp.

After everything done, I used range to query doc in date range. But nothing return. After some researching, I thought the problem was: python timestamp int(time.time()) is 10 digit, but it is 13 digit in elasticsearch , official example :

PUT my_index/my_type/2?timestamp=1420070400000.

so I tried to update the update_time field by multiple 1000:

{
  "script": {
    "inline": "ctx._source.update_time=ctx._source.update_time*1000"
  }
}

Unfortunately, I found all update_time become minus. Then I come up with that Java int type is pow(2,32) -1 , much lower than 1420070400000. So I guess timestamp field should not be int ?

Then I want to update the field value from int to string(I don't want ot change field mapping type, I know change mapping type need reindex, but here only need change value )

But I can't figure out what script I can use, official script document not mention about this

Mithril
  • 12,947
  • 18
  • 102
  • 153

1 Answers1

0

I didn't know groovy was a lauguage and thought it only used for math method because ES doc only wrote about that.

The sulotion is simple, just convert int to long.

curl -XPOST http://localhost:9200/my_index/_update_by_query -d '
{
  "script": {
    "inline": "ctx._source.update_time=(long)ctx._source.update_time*1000"
  }
}'
Mithril
  • 12,947
  • 18
  • 102
  • 153