# Need help on arithmetic operators usage in ingestion / scripted fields

**URL:** <https://discuss.elastic.co/t/need-help-on-arithmetic-operators-usage-in-ingestion-scripted-fields/247660>\
**Category:** Elasticsearch\
**Created:** [September 6, 2020, 8:26am UTC](https://discuss.elastic.co/t/need-help-on-arithmetic-operators-usage-in-ingestion-scripted-fields/247660 "2020-09-06T08:26:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prabhakar\_talari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prabhakar_talari/32/46469_2.png) [@prabhakar\_talari](https://discuss.elastic.co/u/prabhakar_talari)\
**Post date:** [September 6, 2020, 8:26am UTC](https://discuss.elastic.co/t/need-help-on-arithmetic-operators-usage-in-ingestion-scripted-fields/247660/1 "2020-09-06T08:26:30Z")

</div>

Hi All,

I have a field called duration which is number ( in seconds ). I would like to write ingestion pipeline OR scripted field add new field called duration\_hr by dividing the duration with 3600. my ES version is 7.6 also i could see in scripted field only painless language is supported same for ingestion pipeline as well.

Also, i do not want to use any Kibana visualization for this i am primarily focusing on how i can add new field to document which is duraiton\_hr.

Could some one suggest how could i achieve this

Thanks

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [September 6, 2020, 9:00am UTC](https://discuss.elastic.co/t/need-help-on-arithmetic-operators-usage-in-ingestion-scripted-fields/247660/2 "2020-09-06T09:00:37Z")

</div>

**[Note for scripted fields](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html)**

Computing data on the fly with scripted fields can be very resource intensive and can have a direct impact on Kibana performance. Keep in mind that there’s no built-in validation of a scripted field. If your scripts are buggy, you’ll get exceptions whenever you try to view the dynamically generated data.

This can be achiedved like this :

Example of index

```
PUT my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "call_duration_ms": {
        "type": "long"
      },
       "call_duration_h": {
        "type": "long"
      }
    }
  }
}

```

Example of document ingested

```
POST my_index/_doc
{
  "@timestamp": "2020-09-06T09:48:10.000",
  "call_duration_ms": 7200
}

```

This is an example of search query that add a scripted field on the flight during the search (the scripted field can be added in kibana on the index pattern used with the code : `doc['call_duration_ms'].value / 3600.00`

```
GET my_index/_search
{
  "_source": [
    "@timestamp",
    "call_duration"
  ],
  "script_fields": {
    "duration_h": {
      "script": {
        "source": "doc['call_duration_ms'].value / params.factor",
        "params": {
          "factor": 3600.00
        }
      }
    }
  }
}

```

I would suggest an ingest pipeline as it intercept documents and compute the field then ingest ...

```
PUT _ingest/pipeline/my_index
{
  "description": "this is an example to get duration in hour",
  "processors": [
    {
      "script": {
        "lang": "painless",
        "source": "ctx.call_duration_h = ctx.call_duration_ms / params.factor",
        "params": {
          "factor": 3600.00
        }
      }
    }
  ]
}

```

The ingest pipeline can be trigged when ingesting documents like this :

```
POST my_index/_doc?pipeline=my_index
{
  "@timestamp": "2020-09-06T10:48:10.000",
  "call_duration": 9200
}

```

Or it can setted up in the [settings](https://www.elastic.co/guide/en/elasticsearch/reference/master/index-modules.html#dynamic-index-settings) of your index

```
PUT my_index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "index.default_pipeline": "my_index",
    "index.final_pipeline": "foo"

  },
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "call_duration_ms": {
        "type": "long"
      },
       "call_duration_h": {
        "type": "long"
      }
    }
  }
}

```

If you are using logstash as ingest tool, i prefer using a ruby filter to that calculation

---

<div class="post-metadata">

**Author:** ![prabhakar\_talari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prabhakar_talari/32/46469_2.png) [@prabhakar\_talari](https://discuss.elastic.co/u/prabhakar_talari)\
**Post date:** [September 6, 2020, 1:29pm UTC](https://discuss.elastic.co/t/need-help-on-arithmetic-operators-usage-in-ingestion-scripted-fields/247660/4 "2020-09-06T13:29:10Z")

</div>

Thank you so much.. i have implemented ingestion pipeline things looks good.

---

<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:** [October 4, 2020, 1:29pm UTC](https://discuss.elastic.co/t/need-help-on-arithmetic-operators-usage-in-ingestion-scripted-fields/247660/5 "2020-10-04T13:29:16Z")

</div>

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