# Calculate two timestamps using painless

**URL:** <https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556>\
**Category:** Elasticsearch\
**Created:** [October 9, 2018, 6:29am UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556 "2018-10-09T06:29:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shwesinhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shwesinhan/32/78663_2.png) [@shwesinhan](https://discuss.elastic.co/u/shwesinhan)\
**Post date:** [October 9, 2018, 6:29am UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/1 "2018-10-09T06:29:11Z")

</div>

hi team!

is that possible to calculate subtract the time between two logs by id in painless script???  
log example is below:

 ![table](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a2fa2fcaeb7aff7bb36fba64699afc8933b29542.png)

plz point out to me if possible 🙂  
thanks so much

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 9, 2018, 8:17am UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/2 "2018-10-09T08:17:05Z")

</div>

Scripted field run in the context of a single document, so can not act on information from different documents.

---

<div class="post-metadata">

**Author:** ![shwesinhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shwesinhan/32/78663_2.png) [@shwesinhan](https://discuss.elastic.co/u/shwesinhan)\
**Post date:** [October 9, 2018, 8:54am UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/3 "2018-10-09T08:54:10Z")

</div>

thank you @Christian_Dahlqvist

so it's still challenging flat-world to emulate real-world 😢

---

<div class="post-metadata">

**Author:** ![shwesinhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shwesinhan/32/78663_2.png) [@shwesinhan](https://discuss.elastic.co/u/shwesinhan)\
**Post date:** [October 11, 2018, 2:11am UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/4 "2018-10-11T02:11:06Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> n the context of a single document,

Also Scripted Metric Aggregation???

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 11, 2018, 5:23am UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/5 "2018-10-11T05:23:45Z")

</div>

That may be able to do it, but as far as I recall Kibana does not yet support it. Another option would be to create an [entity-centric index](https://www.elastic.co/videos/entity-centric-indexing-mark-harwood) as described in [this talk](https://www.youtube.com/watch?v=yBf7oeJKH2Y).

---

<div class="post-metadata">

**Author:** ![shwesinhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shwesinhan/32/78663_2.png) [@shwesinhan](https://discuss.elastic.co/u/shwesinhan)\
**Post date:** [October 11, 2018, 6:07am UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/7 "2018-10-11T06:07:28Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 14, 2018, 9:26pm UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/8 "2018-10-14T21:26:14Z")

</div>

Scripted metric aggregations operate just as other aggregations: one document at a time. While it might be possible to stash data between map operations, the order would be inconsistent as documents move between Lucene segments.

---

<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:** [November 11, 2018, 9:26pm UTC](https://discuss.elastic.co/t/calculate-two-timestamps-using-painless/151556/9 "2018-11-11T21:26:16Z")

</div>

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