# Efficient date difference at document upsert

**URL:** https://discuss.elastic.co/t/efficient-date-difference-at-document-upsert/119823
**Category:** Elasticsearch
**Created:** [February 14, 2018, 3:04pm UTC](https://discuss.elastic.co/t/efficient-date-difference-at-document-upsert/119823 "2018-02-14T15:04:24Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DidierB](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@DidierB](https://discuss.elastic.co/u/DidierB)
#### Post date: [February 14, 2018, 3:04pm UTC](https://discuss.elastic.co/t/efficient-date-difference-at-document-upsert/119823/1 "2018-02-14T15:04:24Z")

</div>

Hello,  
I would like to make thousands of upsert and computing a time difference between the document already indexed (not with a time-based key but with an id key) and an already stored date, so I can tell what was the last time between two connections.

I would like to do something like this in my script:  
`ctx._source.diffdate = params.infodate.epoch_second - ctx._source.lastinfodate.epoch_second`

Where params.infodate contains the latest timestamp and the ctx.\_source. **lastinfodate** contains the former latest timestamp.

**In the mapping, the lastinfodate is known as a Date but, in the script, it is known as a String** , so I can't access epoch\_second method.  
`"reason" : "Unable to find dynamic field [epoch_second] for class [java.lang.String]."`

I would like to load the field directly as a number in the script to do the difference. Doing "string to date" conversion in the script would result in an non-affordable CPU time.

How can I do it?

---

<div class="post-metadata">

### Author: ![DidierB](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@DidierB](https://discuss.elastic.co/u/DidierB)
#### Post date: [February 15, 2018, 1:47pm UTC](https://discuss.elastic.co/t/efficient-date-difference-at-document-upsert/119823/2 "2018-02-15T13:47:22Z")

</div>

Well, at the injection I used a timestamp conversion in integer so I do arithmetics.

---

<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: [March 15, 2018, 1:47pm UTC](https://discuss.elastic.co/t/efficient-date-difference-at-document-upsert/119823/3 "2018-03-15T13:47:29Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
