Painless: is it possible for a def variable to contain nested objects?

I'm trying to store a nested key/value object in a variable, but it doesn't seem to be storing anything.

In a for loop, where "i" is the index:
def ratio = params['_source']['ratios'][i]

...def will be null.

Is it not possible to assign nested values/objects to variables?

Mapping of "ratios":

                    "ratios": {
                        "type": "nested",
                        "properties": {
                            "Key": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            },
                            "Value": {
                                "type": "float"
                            }
                        }
                    }

Which context (search/update/script field/etc.) are you using this in? Could you provide a more helpful repro?

Sure Igor - I'm trying to assign a nested object to a "def" variable when sorting a search result.

An example of the data I am trying to sort:

"ratios": [
     {
       {
         "Key": "FirstKey",
         "Value": 0.4898
       },
       {
         "Key": "SecondKey",
         "Value": 0.14286
       },
       {
         "Key": "ThirdKey",
         "Value": 0.12245
       },
     ...
     }
 ]

I am looping through these nested objects above with the following Painless code:

for (int i = 0; i < params['_source']['ratios'].length; i++) { 
  def ratio = params['_source']['ratios'][i];
  return ratio.Key; // return immediately to test the ratio variable
}

I consistently get [null] returned above.

Am I doing anything wrong, or can a "def" variable not contain a nested object?

Thanks.

Any chance you can show a full example with mapping and some data I can actually run?

You know what Igor, I just figured it out. I was under the impression that the return value in Painless would set the _score value. I see it in stead returns a "sort" array. My mistake.

Thank you for your replies, and have a great day!

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