# Create a field by doing arithmetic operation on two fileds

**URL:** <https://discuss.elastic.co/t/create-a-field-by-doing-arithmetic-operation-on-two-fileds/83820>\
**Category:** Elasticsearch\
**Created:** [April 27, 2017, 8:40am UTC](https://discuss.elastic.co/t/create-a-field-by-doing-arithmetic-operation-on-two-fileds/83820 "2017-04-27T08:40:38Z")\
**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:** [April 27, 2017, 8:40am UTC](https://discuss.elastic.co/t/create-a-field-by-doing-arithmetic-operation-on-two-fileds/83820/1 "2017-04-27T08:40:39Z")

</div>

```
{
    "actualdatetime": "2016-May-19 00:00:00.965 +05:30",
    "messagedeliverytime": "Thu May 19 00:00:00 2016",
    "messagesubmissiontime": "Wed May 18 23:59:13 2016"
}

```

This is my actual document. I need a new field "latency" by doing

`"(doc['messagedeliverytime'].value - doc['messagesubmissiontime'].value) / 1000"`

And insert it as

```
{
    "actualdatetime": "2016-May-19 00:00:00.965 +05:30",
    "messagedeliverytime": "Thu May 19 00:00:00 2016",
    "messagesubmissiontime": "Wed May 18 23:59:13 2016",
   "latency":"47"
}

```

Is there any scripting which will run the calculation while indexing the documents and create the new field on the fly???

---

<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:** [April 27, 2017, 2:48pm UTC](https://discuss.elastic.co/t/create-a-field-by-doing-arithmetic-operation-on-two-fileds/83820/2 "2017-04-27T14:48:23Z")

</div>

You should be able to do it with the [script](https://www.elastic.co/guide/en/elasticsearch/reference/current/script-processor.html) ingest processor, though you won't do it with `doc`, it'll look more like `ctx['messagedeliverytime'] - ctx['messagesubmissiontime']` though you might need to do date parsing because at that point the dates may not be parsed. Have a look [here](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/painless-api-reference.html#painless-api-reference-DateTimeFormatter) for a reference on the available methods for parsing.

---

<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:** [April 27, 2017, 4:19pm UTC](https://discuss.elastic.co/t/create-a-field-by-doing-arithmetic-operation-on-two-fileds/83820/3 "2017-04-27T16:19:23Z")

</div>

Can you show a small example for this??? How to parse before indexing. And how to put script in mapping

---

<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:** [May 25, 2017, 4:31pm UTC](https://discuss.elastic.co/t/create-a-field-by-doing-arithmetic-operation-on-two-fileds/83820/4 "2017-05-25T16:31:41Z")

</div>

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