Sort that involves nested values and non nested values (without iterating nested array)

Hi, I want to execute a sort logic that involves nested and non nested values. But I don't know how to get a particular nested value without iterating the nested array.

Having this mapping:

{
  "mapping":{
    "item":{
      "properties":{
        "title":{ "type":"string" },
        "type":{ "type":"string", "index":"not_analyzed" }
        "createTime":{ "type":"date" },
        "tracks":{
          "type":"nested",
          "properties":{
            "userId":{ "type":"string", "index":"not_analyzed" }
            "viewTime":{ "type":"date" },
          }
        }
      }
    }
  }
}

And I want to execute a sort with this logic

// params userId and currentTime
if (doc['type'].value == 'X') {
    return doc['createTime'].value
}
else {
    // This is the problematic condition
    if (exists doc['tracks.viewTime'].value for userId) {
        return doc['tracks.viewTime'].value for userId
    }
    else {
        return currentTime;
    }
}

Is there any way to do this without iterating all tracks? maybe combining different sorts (script, nested, etc)
And if there is a way to do it with nested values, is there a way to do something similar with parent/child mappings (where each track is a child)?

Thanks, Claudio.