# Update of field created by ruby script

**URL:** https://discuss.elastic.co/t/update-of-field-created-by-ruby-script/261459
**Category:** Elasticsearch
**Created:** [January 18, 2021, 5:48pm UTC](https://discuss.elastic.co/t/update-of-field-created-by-ruby-script/261459 "2021-01-18T17:48:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ggab31](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ggab31/32/82454_2.png) [@ggab31](https://discuss.elastic.co/u/ggab31)
#### Post date: [January 18, 2021, 5:48pm UTC](https://discuss.elastic.co/t/update-of-field-created-by-ruby-script/261459/1 "2021-01-18T17:48:03Z")

</div>

Hi,

I have a logstash ruby filter that creates a few fields composed of two parts in a heartbeat index, this way :

```
ruby {
    code => "
    temp=Time.new;
    event.set('time.second', temp.localtime.strftime('%S'));
    event.set('time.minute', temp.localtime.strftime('%M'));
    event.set('time.hour', temp.localtime.strftime('%H'));
    event.set('time.weekday', temp.localtime.strftime('%w'));
    event.set('time.monthday', temp.localtime.strftime('%d'));
    event.set('time.yearday', temp.localtime.strftime('%j'));
    event.set('time.week', temp.localtime.strftime('%V'));
    event.set('time.month', temp.localtime.strftime('%m'));
    event.set('time.year', temp.localtime.strftime('%Y'));
    "
}

```

I'd like to update one of this field with this command :

```
POST heartbeat-7.5.0-2020.12/_update/hnd1FXcBzJTKllCWg227
{
  "script": {
    "source": "ctx._source.time.second = 59",
    "lang": "painless"
  }
}

```

But I get this error :

```
{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[XXXXX][X.X.X.X:9300][indices:data/write/update[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
        "ctx._source.time.second = 59",
        " ^---- HERE"
      ],
      "script": "ctx._source.time.second = 59",
      "lang": "painless",
      "caused_by": {
        "type": "null_pointer_exception",
        "reason": null
      }
    }
  },
  "status": 400
}

```

If I do the same with another composed field of the index that has not been created by my ruby filter, it works properly.  
Could you please help me to understand and resolve this problem ?

---

<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: [January 18, 2021, 5:59pm UTC](https://discuss.elastic.co/t/update-of-field-created-by-ruby-script/261459/2 "2021-01-18T17:59:40Z")

</div>

Try this way, if you don't want to change the ruby filter

```auto
POST heartbeat-7.5.0-2020.12/_update/hnd1FXcBzJTKllCWg227
{
  "script": {
    "source": "ctx._source['time.second'] = 59",
    "lang": "painless"
  }
}

```

---

<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: [January 18, 2021, 6:01pm UTC](https://discuss.elastic.co/t/update-of-field-created-by-ruby-script/261459/3 "2021-01-18T18:01:30Z")

</div>

The ruby part should be like this, to have nested field ...

```auto
ruby {
    code => "
    temp=Time.new;
    event.set('[time][second]', temp.localtime.strftime('%S'));
    ....
    "
}

```

---

<div class="post-metadata">

### Author: ![ggab31](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ggab31/32/82454_2.png) [@ggab31](https://discuss.elastic.co/u/ggab31)
#### Post date: [January 18, 2021, 6:24pm UTC](https://discuss.elastic.co/t/update-of-field-created-by-ruby-script/261459/4 "2021-01-18T18:24:33Z")

</div>

I didn't know the right syntax. It works perfectly now  
Thanks a lot for your help 🙂

---

<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, 2021, 6:24pm UTC](https://discuss.elastic.co/t/update-of-field-created-by-ruby-script/261459/5 "2021-02-15T18:24:36Z")

</div>

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