Having trouble converting Groovy to Painless

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, 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 :thinking:

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

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

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:

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.

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