# Convert unix timestamp (seconds) to java (milliseconds)

**URL:** <https://discuss.elastic.co/t/convert-unix-timestamp-seconds-to-java-milliseconds/21070>\
**Category:** Elasticsearch\
**Created:** [December 3, 2014, 9:52pm UTC](https://discuss.elastic.co/t/convert-unix-timestamp-seconds-to-java-milliseconds/21070 "2014-12-03T21:52:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nwood888](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@nwood888](https://discuss.elastic.co/u/nwood888)\
**Post date:** [December 3, 2014, 9:52pm UTC](https://discuss.elastic.co/t/convert-unix-timestamp-seconds-to-java-milliseconds/21070/1 "2014-12-03T21:52:10Z")

</div>

Greetings,

My primary data store has a date\_created field stored as a unix timestamp.  
I've verified that if I multiply that value by 1000 and then index  
it, Elasticsearch correctly picks the field up as a date. But updating the  
data in my primary data store is not very feasible, therefore, I'm looking  
for some way to have Elasticsearch do the conversion for me. Lazy, eh?

I've tried adding this to my mapping

* * *

```
    "transform" : {
        "script" : "ctx._source['date_created'] = 

```

## ctx.\_source['date\_created'] \* 1000;", "lang": "groovy" }, "properties" : { ...

I was expecting that to multiply the date\_created field by 1000 prior to  
parsing the field as a date, but it doesn't seem to be working.

Any ideas what I might be doing wrong, or suggestions for an alternate  
approach?

Nick

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5544e84b-06e5-4199-adff-17bd4039ac19%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5544e84b-06e5-4199-adff-17bd4039ac19%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![nwood888](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@nwood888](https://discuss.elastic.co/u/nwood888)\
**Post date:** [December 28, 2014, 12:10am UTC](https://discuss.elastic.co/t/convert-unix-timestamp-seconds-to-java-milliseconds/21070/2 "2014-12-28T00:10:13Z")

</div>

Replying to my own question for other searchers:

My problem had to do with types. The mapping that ended up working is:

{  
"test" : {  
"transform" : {  
"script" : "ctx.\_source['date\_created'] =  
ctx.\_source['date\_created'].toLong() \* 1000",  
"lang": "groovy"  
},  
"properties" : {  
"date\_created" : {"type" : "date"},  
}  
}  
}

Note the "toLong()".

Also, once a script is modified, it appears as though the type has to be  
deleted and the mapping re-applied for the changes to take effect.

Nick

On Wednesday, December 3, 2014 2:52:10 PM UTC-7, Nick Wood wrote:

> Greetings,
> 
> My primary data store has a date\_created field stored as a unix timestamp.  
> I've verified that if I multiply that value by 1000 and then index  
> it, Elasticsearch correctly picks the field up as a date. But updating the  
> data in my primary data store is not very feasible, therefore, I'm looking  
> for some way to have Elasticsearch do the conversion for me. Lazy, eh?
> 
> I've tried adding this to my mapping
> 
> * * *
> 
> ```
> "transform" : {
> "script" : "ctx._source['date_created'] = 
> 
> ```
> 
> ## ctx.\_source['date\_created'] \* 1000;", "lang": "groovy" }, "properties" : { ...
> 
> I was expecting that to multiply the date\_created field by 1000 prior to  
> parsing the field as a date, but it doesn't seem to be working.
> 
> Any ideas what I might be doing wrong, or suggestions for an alternate  
> approach?
> 
> Nick

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/991be6f5-31dc-444c-8748-383b8262d074%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/991be6f5-31dc-444c-8748-383b8262d074%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 28, 2014, 12:41am UTC](https://discuss.elastic.co/t/convert-unix-timestamp-seconds-to-java-milliseconds/21070/3 "2014-12-28T00:41:13Z")

</div>

Transform doesn't change the source, just how it is indexed. I made it that  
way because I figured I'd you want to change the source you can do it on  
the application feeding elasticsearch. Transform is a way to index stuff  
but leave it out of the source. Its copy\_to on steroids.

Another reason transform doesn't change the source is that it'd make  
updates and reindexing (like scan/scroll from one index to another)  
complex.

There is an option that will retransform results on the fly but I don't  
know it offhand. I believe its on the transform page in the docs.

Nik  
On Dec 27, 2014 7:10 PM, "Nick Wood" [nwood888@gmail.com](mailto:nwood888@gmail.com) wrote:

> Replying to my own question for other searchers:
> 
> My problem had to do with types. The mapping that ended up working is:
> 
> {  
> "test" : {  
> "transform" : {  
> "script" : "ctx.\_source['date\_created'] =  
> ctx.\_source['date\_created'].toLong() \* 1000",  
> "lang": "groovy"  
> },  
> "properties" : {  
> "date\_created" : {"type" : "date"},  
> }  
> }  
> }
> 
> Note the "toLong()".
> 
> Also, once a script is modified, it appears as though the type has to be  
> deleted and the mapping re-applied for the changes to take effect.
> 
> Nick
> 
> On Wednesday, December 3, 2014 2:52:10 PM UTC-7, Nick Wood wrote:
> 
> > Greetings,
> > 
> > My primary data store has a date\_created field stored as a unix  
> > timestamp. I've verified that if I multiply that value by 1000 and then  
> > index it, Elasticsearch correctly picks the field up as a date. But  
> > updating the data in my primary data store is not very feasible, therefore,  
> > I'm looking for some way to have Elasticsearch do the conversion for me.  
> > Lazy, eh?
> > 
> > I've tried adding this to my mapping
> > 
> > * * *
> > 
> > ```
> > "transform" : {
> > "script" : "ctx._source['date_created'] =
> > 
> > ```
> > 
> > ## ctx.\_source['date\_created'] \* 1000;", "lang": "groovy" }, "properties" : { ...
> > 
> > I was expecting that to multiply the date\_created field by 1000 prior to  
> > parsing the field as a date, but it doesn't seem to be working.
> > 
> > Any ideas what I might be doing wrong, or suggestions for an alternate  
> > approach?
> > 
> > Nick
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/991be6f5-31dc-444c-8748-383b8262d074%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/991be6f5-31dc-444c-8748-383b8262d074%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/991be6f5-31dc-444c-8748-383b8262d074%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/991be6f5-31dc-444c-8748-383b8262d074%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2E8qCT7GRfLYJo\_GzB\_5x%2BC%3D\_FuJ49M%2BNfD%3D5Lhi4zCQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2E8qCT7GRfLYJo_GzB_5x%2BC%3D_FuJ49M%2BNfD%3D5Lhi4zCQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:41am UTC](https://discuss.elastic.co/t/convert-unix-timestamp-seconds-to-java-milliseconds/21070/4 "2017-07-06T00:41:45Z")

</div>


