Sorting root documents based on field in nested documents

Here is the code that I used.

"sort": {
    "_script": {
        "type": "number",
        "mode": "min",
        "script": {
            "source": "doc['facilities.cost']",
            "lang": "expression"
        },
        "order": "asc",
        "nested": {
          "path": "facilities"
        }
    }
}