# Painless script not work

**URL:** <https://discuss.elastic.co/t/painless-script-not-work/293957>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [January 11, 2022, 12:15am UTC](https://discuss.elastic.co/t/painless-script-not-work/293957 "2022-01-11T00:15:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jongz\_Puangput](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jongz_puangput/32/91740_2.png) [@Jongz\_Puangput](https://discuss.elastic.co/u/Jongz_Puangput)\
**Post date:** [January 11, 2022, 12:15am UTC](https://discuss.elastic.co/t/painless-script-not-work/293957/1 "2022-01-11T00:15:05Z")

</div>

I try to [add runtime field](https://www.elastic.co/guide/en/kibana/current/discover.html#add-field-in-discover) to the current index.

Name: store\_hostname  
Value:

```auto
if (doc['message.keyword'].size() == 0) return;
def source = doc['message.keyword'].value;
if (source != null) {
    Matcher matcher = /((thas1z|mypmfs)\d{4}.(my.foo.com|th-foo.org))/i.matcher(source);
    if (matcher.find()) {
        emit(matcher.group());
        return;
    }
}
emit('no data');

```

But it didn't work.. I also try to check with "Painless Lab" in the Kibana with below script and it success. so I don't know what is the issue.

 ![Screenshot 2022-01-11 071341](https://us1.discourse-cdn.com/elastic/original/3X/e/3/e3a9a4cb146ce2636728f41f81e7594cac85ca28.png)

```auto
def source = 'FooTokenService - Error Response from Foo Token : {"request":{"method":"POST","uri":"http://thas1z2911.th-foo.org:5558/api/Login","headers":{},"body":{"username":"bar","password":"..."}},"response":null,"error":"UnknownHostException : thas1z2911.th-foo.org: Try again","method":"com.foo.xxx.yyy.service.FooTokenService.requestAccessToken(...)"}';

if (source != null) {
    Matcher matcher = /((thas1z|mypmfs)\d{4}.(my.foo.com|th-foo.org))/i.matcher(source);
    if (matcher.find()) {
        return matcher.group();
    }
}
return ('no data');

```

---

<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 8, 2022, 12:15am UTC](https://discuss.elastic.co/t/painless-script-not-work/293957/2 "2022-02-08T00:15:26Z")

</div>

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