# Exception in Kibana Scripted Field

**URL:** <https://discuss.elastic.co/t/exception-in-kibana-scripted-field/132801>\
**Category:** Elasticsearch\
**Created:** [May 22, 2018, 11:32am UTC](https://discuss.elastic.co/t/exception-in-kibana-scripted-field/132801 "2018-05-22T11:32:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShifaliGarg](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@ShifaliGarg](https://discuss.elastic.co/u/ShifaliGarg)\
**Post date:** [May 22, 2018, 11:32am UTC](https://discuss.elastic.co/t/exception-in-kibana-scripted-field/132801/1 "2018-05-22T11:32:30Z")

</div>

I am trying to add scripted field in Kibana using the script from below link under 'Date math resulting in strings'.

> **[Using Painless in Kibana scripted fields
	  	 | Elastic](https://www.elastic.co/blog/using-painless-kibana-scripted-fields)**
>
> Kibana provides powerful ways to search and visualize data stored in Elasticsearch. For the purpose of visualizations, Kibana looks for fields defined in Elasticsearch mappings and presents them as op...

  
Below is the Scrpit where @timestamp is Time filter field::

```auto
LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['@timestamp'].value), ZoneId.of('Z')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())

```

I am able to execute the script in Kibana 5.0.0 but in Kibana 6.2.2 this script is giving below exception.  
Exception StackTrace:

```auto
[2018-05-22T02:44:56,620][DEBUG][o.e.a.s.TransportSearchAction] [SwkE1nT] [601379] Failed to execute fetch phase
org.elasticsearch.transport.RemoteTransportException: [SwkE1nT][192.168.140.34:9300][indices:data/read/search[phase/fetch/id]]
Caused by: org.elasticsearch.script.ScriptException: runtime error
        at org.elasticsearch.painless.PainlessScript.convertToScriptException(PainlessScript.java:101) ~[?:?]
        at org.elasticsearch.painless.PainlessScript$Script.execute(LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['analysisdate'].value), ZoneId.of('Z')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault()):104) ~[?:?]
        at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:105) ~[?:?]
        at org.elasticsearch.search.fetch.subphase.ScriptFieldsFetchSubPhase.hitsExecute(ScriptFieldsFetchSubPhase.java:67) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:170) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.search.SearchService.executeFetchPhase(SearchService.java:499) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.action.search.SearchTransportService$11.messageReceived(SearchTransportService.java:444) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.action.search.SearchTransportService$11.messageReceived(SearchTransportService.java:441) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler$1.doRun(SecurityServerTransportInterceptor.java:258) ~[?:?]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.common.util.concurrent.EsExecutors$1.execute(EsExecutors.java:135) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler.lambda$messageReceived$0(SecurityServerTransportInterceptor.java:307) ~[?:?]
        at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:60) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile.lambda$inbound$2(ServerTransportFilter.java:166) ~[?:?]
        at org.elasticsearch.xpack.security.authz.AuthorizationUtils$AsyncAuthorizer.maybeRun(AuthorizationUtils.java:183) ~[?:?]
        at org.elasticsearch.xpack.security.authz.AuthorizationUtils$AsyncAuthorizer.setRunAsRoles(AuthorizationUtils.java:177) ~[?:?]
        at org.elasticsearch.xpack.security.authz.AuthorizationUtils$AsyncAuthorizer.authorize(AuthorizationUtils.java:165) ~[?:?]
        at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile.lambda$inbound$3(ServerTransportFilter.java:168) ~[?:?]
        at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:60) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$authenticateAsync$2(AuthenticationService.java:184) ~[x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$lookForExistingAuthentication$4(AuthenticationService.java:217) ~[x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lookForExistingAuthentication(AuthenticationService.java:228) [x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.authenticateAsync(AuthenticationService.java:182) [x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.access$000(AuthenticationService.java:143) [x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.authc.AuthenticationService.authenticate(AuthenticationService.java:113) [x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile.inbound(ServerTransportFilter.java:142) [x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler.messageReceived(SecurityServerTransportInterceptor.java:314) [x-pack-security-6.2.2.jar:6.2.2]
        at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:66) [elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:656) [elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:672) [elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:41) [elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.2.2.jar:6.2.2]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
Caused by: java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(Dates)ReadableDateTime to (Object)long
        at java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:775) ~[?:1.8.0_121]
        at java.lang.invoke.MethodHandle.asType(MethodHandle.java:761) ~[?:1.8.0_121]
        at org.elasticsearch.painless.DefBootstrap$PIC.fallback(DefBootstrap.java:203) ~[?:?]
        at org.elasticsearch.painless.PainlessScript$Script.execute(LocalDateTime.ofInstant(Instant.ofEpochMilli(doc['analysisdate'].value), ZoneId.of('Z')).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault()):65) ~[?:?]
        at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:105) ~[?:?]
        at org.elasticsearch.search.fetch.subphase.ScriptFieldsFetchSubPhase.hitsExecute(ScriptFieldsFetchSubPhase.java:67) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:170) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.search.SearchService.executeFetchPhase(SearchService.java:499) ~

```

---

<div class="post-metadata">

**Author:** ![ppisljar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppisljar/32/11588_2.png) [@ppisljar](https://discuss.elastic.co/u/ppisljar)\
**Post date:** [May 22, 2018, 3:47pm UTC](https://discuss.elastic.co/t/exception-in-kibana-scripted-field/132801/2 "2018-05-22T15:47:43Z")

</div>

seems to be related to elastic search so i am moving this to es forum

did anything change with scripted fields in 6.x ?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 22, 2018, 5:19pm UTC](https://discuss.elastic.co/t/exception-in-kibana-scripted-field/132801/3 "2018-05-22T17:19:14Z")

</div>

As of Elasticsearch ~5.4 (I can't remember the exact version), `.value` for a date field returns a `ReadableDateTime`. So add `.millis` to your `doc['@timestamp'].value` and it should work.

---

<div class="post-metadata">

**Author:** ![ShifaliGarg](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@ShifaliGarg](https://discuss.elastic.co/u/ShifaliGarg)\
**Post date:** [May 23, 2018, 10:09am UTC](https://discuss.elastic.co/t/exception-in-kibana-scripted-field/132801/4 "2018-05-23T10:09:27Z")

</div>

Thanks for the reply.

I want to do some manipulations on the date field like adding or subtracting days. when i am trying  
doc['@timestamp'].value.addDays(1)  
it is giving me following exception ::

Caused by: java.lang.IllegalArgumentException: Unable to find dynamic method [addDays] with [1] arguments for class [org.joda.time.MutableDateTime].  
at org.elasticsearch.painless.Def.lookupMethodInternal(Def.java:210) ~[?:?]  
at org.elasticsearch.painless.Def.lookupMethod(Def.java:239) ~[?:?]  
at org.elasticsearch.painless.DefBootstrap$PIC.lookup(DefBootstrap.java:147) ~[?:?]  
at org.elasticsearch.painless.DefBootstrap$PIC.fallback(DefBootstrap.java:203) ~[?:?]  
at org.elasticsearch.painless.PainlessScript$Script.execute((doc['analysisdate'].value).addDays(1):28) ~[?:?]  
at org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:105) ~[?:?]  
at org.elasticsearch.search.fetch.subphase.ScriptFieldsFetchSubPhase.hitsExecute(ScriptFieldsFetchSubPhase.java:67) ~[elasticsearch-6.2.2.jar:6.2.2]  
at org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:170) ~[elasticsearch-6.2.2.jar:6.2.2]  
at org.elasticsearch.search.SearchService.executeFetchPhase(SearchService.java:499) ~[elasticsearch-6.2.2.jar:6.2.2]  
at org.elasticsearch.action.search.SearchTransportService$11.messageReceived(SearchTransportService.java:444) ~[elasticsearch-6.2.2.jar:6.2.2]

---

<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 20, 2018, 10:09am UTC](https://discuss.elastic.co/t/exception-in-kibana-scripted-field/132801/5 "2018-06-20T10:09:34Z")

</div>

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