# Painless script works in elasticsearch but not kibana

**URL:** https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276
**Category:** Kibana
**Created:** [July 17, 2018, 8:02am UTC](https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276 "2018-07-17T08:02:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Steven\_Ensslen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steven_ensslen/32/26984_2.png) [@Steven\_Ensslen](https://discuss.elastic.co/u/Steven_Ensslen)
#### Post date: [July 17, 2018, 8:02am UTC](https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276/1 "2018-07-17T08:02:46Z")

</div>

Hi,

This works in elasticsearch:

> GET cart-_/\_search  
> {  
> "script\_fields": {  
> "myfield": {  
> "script": {  
> "source": "float balance; if (doc['arrival\_date'] != null && doc['balance\_owing'] != null) { long then = doc['arrival\_date'].value.getMillis(); long now = new Date().getTime(); long days\_old = (now - then) / (1000_60_60_24); if (days\_old \< 30) { balance = doc['balance\_owing'].value; } } return balance;"  
> }}  
> }  
> }

returns:

> {  
> "took": 4,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 17,  
> "successful": 17,  
> "skipped": 0,  
> "failed": 0  
> },  
> "hits": {  
> ...

When I use the same script as a scripted field in Kibana I get a `Courier fetch: 2 of 17 shards failed.` error in _Discover_ and no data. I'm running both in the same [cloud.elastic.co](http://cloud.elastic.co) hosted version 6.3.1 cluster.

Any ideas?

---

<div class="post-metadata">

### Author: ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)
#### Post date: [July 17, 2018, 7:46pm UTC](https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276/2 "2018-07-17T19:46:05Z")

</div>

Hi Steven, have you taken a look at your Elasticsearch logs to learn more about the failure?

One thing which is important to note is that two searches you're referencing are different. When you search in ES you're hitting the `_search` endpoint, you're searching a single index, and you're restricting the response to the values created by your scripted fields. When you search in Discover, you're hitting `_msearch` endpoint, you're searching with an index pattern across all matching indices, and you're getting back a response containing all of the fields within those indices. I'm not an ES guru, but if you can tell me what your ES logs say then I can rope in some colleagues to help.

Thanks,  
CJ

---

<div class="post-metadata">

### Author: ![Steven\_Ensslen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steven_ensslen/32/26984_2.png) [@Steven\_Ensslen](https://discuss.elastic.co/u/Steven_Ensslen)
#### Post date: [July 18, 2018, 5:43am UTC](https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276/3 "2018-07-18T05:43:04Z")

</div>

Thanks CJ!

MSearch works when I call it properly:

> GET cart-_/\_msearch  
> {}  
> {"script\_fields": {"myfield": {"script": {"source": "float balance; if (doc['arrival\_date'] != null && doc['balance\_owing'] != null) { long then = doc['arrival\_date'].value.getMillis(); long now = new Date().getTime(); long days\_old = (now - then) / (1000_60_60_24); if (days\_old \< 30) { balance = doc['balance\_owing'].value; } } return balance;"}}}}

The logs say only this and the snapshot INFO messages:

> Jul 18, 2018, 5:39:55 AM UTC [2018-07-18T05:39:55,396][WARN][org.elasticsearch.deprecation.common.ParseField] Deprecated field [inline] used, expected [source] instead

---

<div class="post-metadata">

### Author: ![Steven\_Ensslen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steven_ensslen/32/26984_2.png) [@Steven\_Ensslen](https://discuss.elastic.co/u/Steven_Ensslen)
#### Post date: [July 19, 2018, 7:48am UTC](https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276/4 "2018-07-19T07:48:30Z")

</div>

Hi @cjcenizal ,

I was able resolve my problem by removing references to the data type `float`. This script works in Kibana:

> long then = doc['arrival\_date'].value.getMillis(); long now = new Date().getTime(); long age = (now - then) / (1000 \* 60 \* 60 \* 24); if (age \< 30) { return doc['balance\_owing'].value }

Steven

---

<div class="post-metadata">

### Author: ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)
#### Post date: [July 19, 2018, 5:37pm UTC](https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276/5 "2018-07-19T17:37:55Z")

</div>

Thanks for sharing your solution, Steven! It's great you were able to figure it out.

CJ

---

<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: [August 16, 2018, 5:37pm UTC](https://discuss.elastic.co/t/painless-script-works-in-elasticsearch-but-not-kibana/140276/6 "2018-08-16T17:37:59Z")

</div>

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