# Painless: problem accessing nested object using dot notation

**URL:** <https://discuss.elastic.co/t/painless-problem-accessing-nested-object-using-dot-notation/150599>\
**Category:** Elasticsearch\
**Created:** [October 1, 2018, 6:55pm UTC](https://discuss.elastic.co/t/painless-problem-accessing-nested-object-using-dot-notation/150599 "2018-10-01T18:55:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rozling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rozling/32/16401_2.png) [@rozling](https://discuss.elastic.co/u/rozling)\
**Post date:** [October 1, 2018, 6:55pm UTC](https://discuss.elastic.co/t/painless-problem-accessing-nested-object-using-dot-notation/150599/1 "2018-10-01T18:55:56Z")

</div>

Hi,

I'm having problems reindexing data using a painless script.

The nested field `context.data.result` is mapped as an object in the destination index, but in some cases the source data is either a string or an array of longs

The script attempts to:

1. Check if the field exists
2. Check the field's type
3. If it is a string or array
  - create a temporary string / array variable
  - assign the value of the offending field to this variable
  - change the value of the field to be a new object (passed in via params) using a `put` command
  - assign the value of the temp variable to the first key/value pair of the new object

This works fine for strings, but it's not working for arrays.

Here's one document it fails on:

```json
{
    "_index": "logstash-crm-2018.04.29",
    "_type": "doc",
    "_id": "ssNuEGMBPRb2-UEOO0NF",
    "_version": 1,
    "found": true,
    "_source": {
        "context": {
            "data": {
                "result": [
                    3433229,
                    3433301,
                    2993663
                ],
                "resultCount": 3
            },
            "message": "success",
            "status": 200
        },
        "message": "DOCUMENT_VALIDATIONS_NEEDED",
        "type": "log",
        "level": 200,
        "log_type": "json",
        "source": "/var/www/our-crm/log/our-crm-2018-04-29.log",
        "datetime": {
            "timezone_type": 3,
            "date": "2018-04-29 09:04:08",
            "timezone": "Europe/London"
        },
        "@version": "1",
        "level_name": "INFO",
        "tags": [
            "beats_input_codec_plain_applied"
        ],
        "@timestamp": "2018-04-29T08:04:09.437Z",
        "input_type": "log",
        "service": "crm",
        "offset": 82722,
        "beat": {
            "version": "5.6.8",
            "name": "aws-hostname",
            "hostname": "aws-hostname"
        },
        "log_source": "file"
    }
}

```

And here's my Painless script:

```auto
{
  "source": {
    "index": ["{{source}}"]
  },
  "dest": {
    "index": "{{dest}}"
  },
  "script": {
    "source": """
    if (ctx._source.context != null) {
        if (ctx._source.context instanceof String) {
            String name = ctx._source.context;
            ctx._source.put(\"context\", params.context);
            ctx._source.context.name = name 
        } else if (ctx._source.context.data != null) {
            if (ctx._source.context.data.result instanceof Long) {
                Long number = ctx._source.context.data.result;
                ctx._source.put(\"context.data.result\", params.result);
                ctx._source.context.data.result.number = number 
            } else if (ctx._source.context.data.result instanceof List) {
                    long[] longArray = new long[ctx._source.context.data.result.length];
                    longArray = ctx._source.context.data.result;
                    ctx._source.put(\"context.data.result\", params.result2);
                    ctx._source.context.data.result.longArray = longArray 
            } 
        } 
    }
    """,
    "lang": "painless",
     "params": {
     "context": { "name": "blank" },
     "result": { "number": 1234 },
     "result2": { "longArray": [1234, 5678] }
    }
  }
}

```

Currently Painless throws an error at this point in the code:

```auto
            "if (ctx._source.context.data != null) {",
            " ^---- HERE"

```

And the error message is:

```auto
            "type": "illegal_argument_exception",
            "reason": "Illegal list shortcut value [data]."

```

I'm not sure why this would be the case, since I know the source data contains `context.data.result`?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 3, 2018, 11:35pm UTC](https://discuss.elastic.co/t/painless-problem-accessing-nested-object-using-dot-notation/150599/2 "2018-10-03T23:35:06Z")

</div>

It looks like `ctx._source.context` is an array in your source. So you need to iterate through each value of the array and dereference it by index. So instead of `ctx._source.context.data`, you need a loop like:

```auto
for (int i = 0; i < ctx._source.context.length; ++i) {
  ctx._source.context[i].data
}

```

---

<div class="post-metadata">

**Author:** ![rozling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rozling/32/16401_2.png) [@rozling](https://discuss.elastic.co/u/rozling)\
**Post date:** [October 5, 2018, 1:38pm UTC](https://discuss.elastic.co/t/painless-problem-accessing-nested-object-using-dot-notation/150599/3 "2018-10-05T13:38:43Z")

</div>

Hi Ryan, thanks for your reply!

`result` is definitely an array but `context` is an object, unless I'm misunderstanding something?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 14, 2018, 10:53pm UTC](https://discuss.elastic.co/t/painless-problem-accessing-nested-object-using-dot-notation/150599/4 "2018-10-14T22:53:36Z")

</div>

Are you sure _all_ of your documents have `context` as an object? The error from painless indicates it found a List.

---

<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:** [November 11, 2018, 10:53pm UTC](https://discuss.elastic.co/t/painless-problem-accessing-nested-object-using-dot-notation/150599/5 "2018-11-11T22:53:38Z")

</div>

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