# Can not access array with objects in painless

**URL:** <https://discuss.elastic.co/t/can-not-access-array-with-objects-in-painless/198226>\
**Category:** Elasticsearch\
**Created:** [September 5, 2019, 10:44am UTC](https://discuss.elastic.co/t/can-not-access-array-with-objects-in-painless/198226 "2019-09-05T10:44:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sailxjx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sailxjx/32/53625_2.png) [@sailxjx](https://discuss.elastic.co/u/sailxjx)\
**Post date:** [September 5, 2019, 10:44am UTC](https://discuss.elastic.co/t/can-not-access-array-with-objects-in-painless/198226/1 "2019-09-05T10:44:45Z")

</div>

I indexed some documents with the following structure:

```auto
{
          "id" : "5d528eb2585e760011d88f84",
          "visits" : [
            {
              "visitCount" : 2,
              "recentlyVisitTime" : 1566524283718,
              "userId" : "59e80aa28dcb4e0001104b54"
            },
            {
              "visitCount" : 2,
              "recentlyVisitTime" : 1566960280913,
              "userId" : "55c060be2944cfe95e75a088"
            }
          ]

```

When I wanted to score the documents with painless script, an error appeared.  
This is my script:

```auto
Debug.explain(doc['visits']);
              for (Map x : doc['visits']) {
                Debug.explain(x);
              }

```

This is the error:

```auto

  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
          "Debug.explain(doc['visits']);\n ",
          " ^---- HERE"
        ],
        "script": " Debug.explain(doc['visits']);\n for (Map x : doc['visits.userId'].value) {\n Debug.explain(x);\n }\n 1;",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "tasks",
        "node": "71KdK5vZQZ-Nm3iRxn0wVg",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
            "Debug.explain(doc['visits']);\n ",
            " ^---- HERE"
          ],
          "script": " Debug.explain(doc['visits']);\n for (Map x : doc['visits.userId'].value) {\n Debug.explain(x);\n }\n 1;",
          "lang": "painless",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [visits] in mapping with types []"
          }
        }
      }
    ]
  },
  "status": 500
}

```

---

<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:** [September 13, 2019, 6:18pm UTC](https://discuss.elastic.co/t/can-not-access-array-with-objects-in-painless/198226/2 "2019-09-13T18:18:15Z")

</div>

What you are trying to do is not currently possible. Accessing nested fields through doc values is not supported. There is a [long standing open issue](https://github.com/elastic/elasticsearch/issues/23719) to at least give a better error message. You will need to look at your raw document with `_source`, or serialize the data you want into a field of its own.

---

<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:** [October 11, 2019, 6:22pm UTC](https://discuss.elastic.co/t/can-not-access-array-with-objects-in-painless/198226/3 "2019-10-11T18:22:14Z")

</div>

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