# Having trouble converting Groovy to Painless

**URL:** <https://discuss.elastic.co/t/having-trouble-converting-groovy-to-painless/176118>\
**Category:** Elasticsearch\
**Created:** [April 9, 2019, 10:26pm UTC](https://discuss.elastic.co/t/having-trouble-converting-groovy-to-painless/176118 "2019-04-09T22:26:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw-ovgd](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@jw-ovgd](https://discuss.elastic.co/u/jw-ovgd)\
**Post date:** [April 9, 2019, 10:26pm UTC](https://discuss.elastic.co/t/having-trouble-converting-groovy-to-painless/176118/1 "2019-04-09T22:26:31Z")

</div>

Hi I've been having a very hard time converting an old Groovy script to Painless in a filter query script.

Almost every index in my ES6.5 has a field of this structure:

> ...,  
> groups: [  
> [a],  
> [a, b],  
> [a, b, c]  
> ],  
> ...

I need to search this list, and check the last element of each array for equality to the parameter passed in. Previously my script was one line:

`ctx._source.groups.find{it.last() == groupUri} != null`

I understand that I can no longer access my "groups" field via "ctx.\_source".  
My understanding is I have to use doc.get('groups'), which I think should return to me an array of arrays, but it doesn't look like that is happening.  
doc.get('groups')[0] is an index OOB, doc.get('groups').values[0] and doc.get('groups').value[0] is also an index OOB.

So there's something I am missing here. Does doc.get('groups') not return the array of arrays that I can then iterate over?

The script I have come up with is:

> ```
> if(doc.containsKey('groups') {
> def groups = doc.get('groups');
> if(groups != null && groups.size() > 0) {
> return groups.containsAll(Collections.singleton(params.groupUri));
> }
> }
> 
> ```

But it is too permissive and will return true if an element exists in ANY position in ANY of the arrays.

Seems Groovy was less painless 🤔

---

<div class="post-metadata">

**Author:** ![jw-ovgd](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@jw-ovgd](https://discuss.elastic.co/u/jw-ovgd)\
**Post date:** [April 10, 2019, 3:38pm UTC](https://discuss.elastic.co/t/having-trouble-converting-groovy-to-painless/176118/2 "2019-04-10T15:38:08Z")

</div>

My script now is:

> ```
> if(doc.containsKey('groups') { 
> def groups = doc.get('groups');
> for(int i = 0; i < groups.size(); i++) {
> def group = groups.get(i);
> if(group[group.length - 1] == params.groupUri) {
> return true;
> }
> }
> }
> 
> ```

Which I think should work but I get this exception:

> ```
> "if(group[group.length - 1] == params.groupUri) {",
> " ^---- HERE"
> "type" : "illegal_argument_exception",
> "reason" : "dynamic getter [java.lang.String, length] not found"
> 
> ```

I'm confused why at this point, "group" is being treated as a string? I think it should be an array because "groups" in my indices is an array of arrays

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 11, 2019, 3:11pm UTC](https://discuss.elastic.co/t/having-trouble-converting-groovy-to-painless/176118/3 "2019-04-11T15:11:30Z")

</div>

try using `params._source["groups"]` instead of access doc values

---

<div class="post-metadata">

**Author:** ![jw-ovgd](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@jw-ovgd](https://discuss.elastic.co/u/jw-ovgd)\
**Post date:** [April 11, 2019, 7:23pm UTC](https://discuss.elastic.co/t/having-trouble-converting-groovy-to-painless/176118/4 "2019-04-11T19:23:06Z")

</div>

When I try to access params.\_source I get a null\_pointer\_exception.

This script is being executed on a search and with Painless.

This discussion:

> [@Accessing \_source from script search in 5.0](https://discuss.elastic.co/t/accessing--source-from-script-search-in-5-0/64358):
>
> Hey all, I think I'm going crazy... Could someone tell me why I fail to access the \_source field on this query? { "size": 0, "query": { "constant\_score": { "filter": { "script": { "script": { "inline": "\_source.lab\_tests.length \> 0" } } } } } } I get... { "type": "script\_exception", "reason": "compile error", "caused\_by": { "type": "illegal\_argument\_exception", "reason": "Variable [\_source] is not de…

Makes it seem like I do not have access to \_source from a Painless search script. Is that the case?

If that is the case, is what I am trying to do futile? The doc value for my "groups" field seems flattened so I would not be able to traverse the data structure as I need to.

---

<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:** [May 9, 2019, 7:23pm UTC](https://discuss.elastic.co/t/having-trouble-converting-groovy-to-painless/176118/5 "2019-05-09T19:23:08Z")

</div>

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