# Add a new field with a calculation on every document insertion

**URL:** <https://discuss.elastic.co/t/add-a-new-field-with-a-calculation-on-every-document-insertion/60898>\
**Category:** Elasticsearch\
**Created:** [September 19, 2016, 2:09pm UTC](https://discuss.elastic.co/t/add-a-new-field-with-a-calculation-on-every-document-insertion/60898 "2016-09-19T14:09:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ravi\_Shanker\_Reddy](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ravi\_Shanker\_Reddy](https://discuss.elastic.co/u/Ravi_Shanker_Reddy)\
**Post date:** [September 19, 2016, 2:09pm UTC](https://discuss.elastic.co/t/add-a-new-field-with-a-calculation-on-every-document-insertion/60898/1 "2016-09-19T14:09:13Z")

</div>

My document looks like

`{ "start_time" : "2016-Sep-04 18:06:24.352", "end_time" : "2016-Sep-04 18:06:28.352" }`

When I inserted this document into the ES I want it to become as

`{ "start_time" : "2016-Sep-04 18:06:24.352", "end_time" : "2016-Sep-04 18:06:28.352", "difference": 2000 }`

The difference is in millisecond. Is there any way top acheive this. I seen scripting, updatebyquery & so on. But I dont get any idea. Please help me with this????

---

<div class="post-metadata">

**Author:** ![Ravi\_Shanker\_Reddy](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ravi\_Shanker\_Reddy](https://discuss.elastic.co/u/Ravi_Shanker_Reddy)\
**Post date:** [September 20, 2016, 8:57am UTC](https://discuss.elastic.co/t/add-a-new-field-with-a-calculation-on-every-document-insertion/60898/2 "2016-09-20T08:57:22Z")

</div>

Is there anyway for this?????

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 20, 2016, 9:50am UTC](https://discuss.elastic.co/t/add-a-new-field-with-a-calculation-on-every-document-insertion/60898/3 "2016-09-20T09:50:52Z")

</div>

For now the only way to do it is on client side. In the next version of elasticsearch (5.0) we are adding capabilities in order to enrich the source, see [https://www.elastic.co/guide/en/elasticsearch/reference/master/ingest.html](https://www.elastic.co/guide/en/elasticsearch/reference/master/ingest.html)

---

<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 5, 2017, 10:18pm UTC](https://discuss.elastic.co/t/add-a-new-field-with-a-calculation-on-every-document-insertion/60898/4 "2017-07-05T22:18:55Z")

</div>


