Convert groovy script to painless ( collect)


(radi) #1

Hell i have this script with collection it's working in groovy But I can't find if there's a way to do this with painless ?

GET exemple/_search
{
"script_fields" : {
"checks" : {
"script" : {
"lang": "groovy",
"inline":
"_source.mebers.collect{it.check}"
}
}
}
}


(Felix Stürmer) #2

Hi @radi,

the collect method still exists on collections, but the lambda function syntax is similar to the java sytax, i.e.

GET example/_search
{
  "script_fields": {
    "checks": {
      "script": {
        "lang": "painless",
        "source": "doc['members'].collect(member -> member.check)"
      }
    }
  }
}

(radi) #3

thenk's for your replay ,
I tested this solution but I have this error
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "[script] unknown field [source], parser not found"
}
],
"type": "illegal_argument_exception",
"reason": "[script] unknown field [source], parser not found"
},
"status": 400
}


(Felix Stürmer) #4

Which version of the Elastic Stack are you running?


(radi) #5

Elastic Stack version 5.4.0


(Felix Stürmer) #6

In that version the key is still called inline, so this should work:

GET example/_search
{
  "script_fields": {
    "checks": {
      "script": {
        "lang": "painless",
        "inline": "doc['members'].collect(member -> member.check)"
      }
    }
  }
}

(radi) #7

already test , and i have this error

"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "invalid sequence of tokens near ['->'].",

I need to use _source ( like in groovy )
but i read this now !
[https://github.com/elastic/elasticsearch/issues/20068](http://painless _source )
_source is definitely a controversial issue, but as of right now, _source is only supported in update scripts within Painless.


(Felix Stürmer) #8

hm, this works for me in Kibana 5.4.3:

GET example/_search
{
  "script_fields": {
    "checks": {
      "script": {
        "lang": "painless",
        "inline": "params._source['members'].collect(member -> member.check)"
      }
    }
  }
}

(system) #10

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