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

**URL:** <https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [January 2, 2019, 2:29pm UTC](https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668 "2019-01-02T14:29:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scarabas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scarabas/32/39356_2.png) [@scarabas](https://discuss.elastic.co/u/scarabas)\
**Post date:** [January 2, 2019, 2:29pm UTC](https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668/1 "2019-01-02T14:29:04Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 2, 2019, 5:04pm UTC](https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668/2 "2019-01-02T17:04:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![scarabas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scarabas/32/39356_2.png) [@scarabas](https://discuss.elastic.co/u/scarabas)\
**Post date:** [January 3, 2019, 8:59am UTC](https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668/3 "2019-01-03T08:59:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 3, 2019, 2:24pm UTC](https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668/4 "2019-01-03T14:24:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![scarabas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scarabas/32/39356_2.png) [@scarabas](https://discuss.elastic.co/u/scarabas)\
**Post date:** [January 3, 2019, 5:49pm UTC](https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668/5 "2019-01-03T17:49:20Z")

</div>

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!

---

<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:** [January 31, 2019, 5:49pm UTC](https://discuss.elastic.co/t/painless-is-it-possible-for-a-def-variable-to-contain-nested-objects/162668/6 "2019-01-31T17:49:21Z")

</div>

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