Painless script works in elasticsearch but not kibana

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
606024); 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 hosted version 6.3.1 cluster.

Any ideas?

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

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
606024); 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

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

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

CJ

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