Elastic painless script: Iteration on all documents

Hello everyone,

In elastic script params[_source ] or doc contexts returns only a single document and it allows us to iterate on fields but I need to get variables from all other documents to do my calculation. How can I do that?

I would be happy if anyone can explain on this example;
Thanks a lot !

GET hockey/_search
{
  "query": {
    "function_score": {
      "script_score": {
        "script": {
          "lang": "painless",
          "source": """
            int total = 0;
            for (int i = 0; i < doc['goals'].length; ++i) {
              total += doc['goals'][i];
            }
            return total;
          """
        }
      }
    }
  }
}

Painless runs in the context of a single document, so you do not have access to other documents.