Can't return number from script_fields?

Hello I am trying to use script_fields.
This is my ES request :
{ "query": { "match_all": {} }, "script_fields": { "test": { "script": { "lang": "painless", "source": "(doc['type'].value=='user') ? 1 : null" } } } }

this returns : Cannot cast from [int] to [java.lang.Object]
when I use "source": "2" or "source": "(doc['type'].value=='user') ? 'test' : null",it works perfectly.

Am I missing something ?
Thank you,

what version are you on? This snippet here worked for me (with an additional return statement)

PUT foo/_doc/1?refresh
{
  "key" : "value"
}

GET foo/_search
{
  "script_fields": {
    "test": {
      "script": {
        "lang": "painless",
        "source": "return doc['key.keyword'].value=='user' ? 1 : null"
      }
    }
  }
}
1 Like

Hello,
Thank you for your very fast reply.
I am using 7.1 version on AWS cloud.
This snippet worked perfectyly for me too. I think the return statement is the missing thing :D.

But this is so strange, with string value I don't need to put return, when I put the number value directly I don't need to put return too. I don't know how to explain this.

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