Can not access array with objects in painless

I indexed some documents with the following structure:

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

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

This is the error:


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

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 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.