Sorting root documents based on field in nested documents

I want to sort root documents according to cost value in nested documents. I tried different approach but failed.

Here the output I want to sort on the basis of cost.

"hits" : [
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003915141",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "J",
      "facilities" : [
        {
          "cost" : 50.92
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003915620",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "A.",
      "facilities" : [
        {
          "cost" : 5823.024
        },
        {
          "cost" : 5894.875
        },
        {
          "cost" : 248.405
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003917147     ",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "L",
      "facilities" : [
        {
          "cost" : 65.58
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003923335",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "S.",
      "facilities" : [
        {
          "cost" : 4744.932
        },
        {
          "cost" : 198.399
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003925074",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "",
      "facilities" : [
        {
          "cost" : 11652.64
        },
        {
          "cost" : 15453.483
        },
        {
          "cost" : 1233.04
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003925355",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "",
      "facilities" : [
        {
          "cost" : 63.328
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003927633",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "D",
      "facilities" : [
        {
          "cost" : 6737.7
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003935925",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "MICHELLE",
      "facilities" : [
        {
          "cost" : 60.429
        },
        {
          "cost" : 53.511
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003942061",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "",
      "facilities" : [
        {
          "cost" : 7986.907
        }
      ]
    }
  },
  {
    "_index" : "providers",
    "_type" : "_doc",
    "_id" : "1003963265",
    "_score" : 1.0,
    "_source" : {
      "middleName" : "LAURETA",
      "facilities" : [
        {
          "cost" : 55.788
        }
      ]
    }
  }
]

I used this.
"sort": [
{
"facilities.cost": {
"order": "asc"
}
}
]

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

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