# How to append dynamic field without affecting original payload?

**URL:** <https://discuss.elastic.co/t/how-to-append-dynamic-field-without-affecting-original-payload/115992>\
**Category:** Elasticsearch\
**Created:** [January 18, 2018, 8:12am UTC](https://discuss.elastic.co/t/how-to-append-dynamic-field-without-affecting-original-payload/115992 "2018-01-18T08:12:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuWatanabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuwatanabe/32/13259_2.png) [@YuWatanabe](https://discuss.elastic.co/u/YuWatanabe)\
**Post date:** [January 18, 2018, 8:12am UTC](https://discuss.elastic.co/t/how-to-append-dynamic-field-without-affecting-original-payload/115992/1 "2018-01-18T08:12:55Z")

</div>

Hello

I would like to get help with Alerting in x-pack.

I want to create a dynamic field which calculates search start time using **ctx.trigger.scheduled\_time**. I thought this can be achieved by using _transform_ but since transform overwrites the original payload it does not work.

How can I append new field? I am imagining assigning a new field , **search\_start\_time** , with below calculation.

`Instant.ofEpochMilli(ctx.trigger.scheduled_time.getMillis()).plus(Duration.ofDays(-1));`

My current watch definition is below.

```
{
  "metadata" : {
    "id" : "TEST_1",
    "scenario" : "TEST",
    "interval" : "1"
  },
  "trigger" : { "schedule" : { "interval" : "10s" }},
  "input" : {
      "search" : {
        "request" : {
          "indices" : ["sample"],
          "body" : {
            "query" : {
              "bool" : {
                "must" : [
                  { "match" : { "message": "session opened for user" } },
                  { "range" : { "@timestamp" : { "gte" : "{{ctx.trigger.scheduled_time}}||-{{ctx.metadata.interval}}M", "lt" : "{{ctx.trigger.scheduled_time}}", "format" : "strict_date_optional_time" } } }
                ]
              }
            },
            "aggs" : {
              "usercount" : {
                "terms" : {
                  "field" : "user"
                },
                "aggs" : {
                  "host" : {
                    "terms" : {
                      "field" : "host"
                    }
                  }
                }
              }
            }
          }
        }
      }
  },
  "condition" : {
    "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}
  },
  "actions" : {
    "transform" : {
      "script" : "ArrayList arrL = new ArrayList(); def dc ; def k_user; def k_host ; for ( int i = 0 ; i < ctx.payload.aggregations.usercount.buckets.length ; i++ ) { dc = ctx.payload.aggregations.usercount.buckets[i].doc_count; k_user = ctx.payload.aggregations.usercount.buckets[i].key; for ( int j = 0 ; j < ctx.payload.aggregations.usercount.buckets[i].host.buckets.length ; j++ ) { k_host = ctx.payload.aggregations.usercount.buckets[i].host.buckets[j].key; arrL.add( k_user + ' ' + dc.toString() + ' ' + k_host ); } } return arrL;"
    },
    "send_email" : {
      "email" : {
        "account" : "standard_account",
        "to" : "yu.w.tennis@gmail.com",
        "subject" : "TEST",
        "body" : "{{ctx.payload.hits.total}} error logs found\n\nScenarioID : {{ctx.metadata.id}}\nScenario : {{ctx.metadata.scenario}}\nSearch Period : {{ctx.payload}}\n {{#ctx.payload._value}}{{.}}\n\n{{/ctx.payload._value}}"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 18, 2018, 8:25am UTC](https://discuss.elastic.co/t/how-to-append-dynamic-field-without-affecting-original-payload/115992/2 "2018-01-18T08:25:19Z")

</div>

hey,

you can just append the original payload again like this

```auto
def payload = ctx.payload;
payload.foo = 'bar';
return payload

```

Hope this helps.

--Alex

---

<div class="post-metadata">

**Author:** ![YuWatanabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuwatanabe/32/13259_2.png) [@YuWatanabe](https://discuss.elastic.co/u/YuWatanabe)\
**Post date:** [January 18, 2018, 8:42am UTC](https://discuss.elastic.co/t/how-to-append-dynamic-field-without-affecting-original-payload/115992/3 "2018-01-18T08:42:00Z")

</div>

Thank you . It worked!

---

<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:** [February 15, 2018, 8:42am UTC](https://discuss.elastic.co/t/how-to-append-dynamic-field-without-affecting-original-payload/115992/4 "2018-02-15T08:42:03Z")

</div>

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