# Convert groovy script to painless ( collect)

**URL:** <https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540>\
**Category:** Kibana\
**Created:** [January 8, 2018, 2:35pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540 "2018-01-08T14:35:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![radi](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@radi](https://discuss.elastic.co/u/radi)\
**Post date:** [January 8, 2018, 2:35pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/1 "2018-01-08T14:35:11Z")

</div>

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}"  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [January 8, 2018, 3:35pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/2 "2018-01-08T15:35:38Z")

</div>

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)"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![radi](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@radi](https://discuss.elastic.co/u/radi)\
**Post date:** [January 8, 2018, 3:45pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/3 "2018-01-08T15:45:51Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [January 8, 2018, 4:29pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/4 "2018-01-08T16:29:14Z")

</div>

Which version of the Elastic Stack are you running?

---

<div class="post-metadata">

**Author:** ![radi](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@radi](https://discuss.elastic.co/u/radi)\
**Post date:** [January 8, 2018, 4:36pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/5 "2018-01-08T16:36:05Z")

</div>

Elastic Stack version 5.4.0

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [January 8, 2018, 4:39pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/6 "2018-01-08T16:39:28Z")

</div>

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)"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![radi](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@radi](https://discuss.elastic.co/u/radi)\
**Post date:** [January 8, 2018, 4:47pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/7 "2018-01-08T16:47:30Z")

</div>

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](https://github.com/elastic/elasticsearch/issues/20068)]([http://painless](http://painless) \_source )  
\_source is definitely a controversial issue, but as of right now, \_source is only supported in update scripts within Painless.

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [January 8, 2018, 4:55pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/8 "2018-01-08T16:55:58Z")

</div>

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)"
      }
    }
  }
}
```

---

<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:** [February 5, 2018, 5:01pm UTC](https://discuss.elastic.co/t/convert-groovy-script-to-painless-collect/114540/10 "2018-02-05T17:01:28Z")

</div>

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