# Calculating fields in logstash on upsert

**URL:** <https://discuss.elastic.co/t/calculating-fields-in-logstash-on-upsert/274371>\
**Category:** Logstash\
**Created:** [May 28, 2021, 7:02pm UTC](https://discuss.elastic.co/t/calculating-fields-in-logstash-on-upsert/274371 "2021-05-28T19:02:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tumbledwyer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tumbledwyer/32/89032_2.png) [@tumbledwyer](https://discuss.elastic.co/u/tumbledwyer)\
**Post date:** [May 28, 2021, 7:02pm UTC](https://discuss.elastic.co/t/calculating-fields-in-logstash-on-upsert/274371/1 "2021-05-28T19:02:35Z")

</div>

Hi

I have a pipeline used to enrich some json data from different sources to build up an index for a report. I would like to do a calculation based on 2 fields from the different sources, but since the sources are different, the fields are not in the same event so I want to use a script to do the calculation on upsert.

My mapping looks like this:

```
{  
  "properties": {
    "objectA": {
      "properties": {
        "fieldA": { "type": "date" }
      }
    },
    "objectB": {
      "properties": {
        "fieldB": { "type": "date" }
      }
    },
    "daysBetween": { "type": "integer" }
  }
}

```

objectA and objectB come in as 2 different events and a common field is used to link them to the same report object. This works just fine when using `doc_as_upsert` and both object A and objectB are in the index. But now I need to calculate the `daysBetween` field and I've tried to use the `script` in the `elasticsearch output` to do the date diff but now my upsert is broken and only the last object that arrived is in the index.

So my logic should look something like this:

```
- No object in index: event=objectA
    => upsert objectA
- objectA in index: event=objectB
    => upsert objectB
    => add field daysBetween=datediff(fieldA-fieldB)

```

...and vice-versa if objectB arrives first

How would I go about doing this?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [May 28, 2021, 7:59pm UTC](https://discuss.elastic.co/t/calculating-fields-in-logstash-on-upsert/274371/2 "2021-05-28T19:59:01Z")

</div>

If you are asking how to do a scripted update that is really an elasticsearch question, not a logstash question.

---

<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:** [June 25, 2021, 7:59pm UTC](https://discuss.elastic.co/t/calculating-fields-in-logstash-on-upsert/274371/3 "2021-06-25T19:59:08Z")

</div>

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