Understanding what value will be used for sorting by nested field if field has a several nested values

I have the following mapping

{
  "mappings": {
    "properties": {
      "rank-by-views": {
        "type": "nested",
        "properties": {
          "id": {"type": "keyword"},
          "value": {"type": "keyword"}
        }
      }
    }
  }
}

And I need to sort bases on rank of specified view or if view rank is not exist for this view use default rank:

{
  "sort": [
    {
      "rank-by-views.value": {
        "order": "desc",
        "nested": {
          "path": "rank-by-views",
          "filter": {
            "bool": {
              "should": [
                {
                  "term": {
                    "rank-by-views.id": "view-1"
                  }
                },
                {
                  "term": {
                    "rank-by-views.id": "default"
                  }
                }
              ]
            }
          }
        }
      }
    }
  ]
}

I have cases when I have default and view rank but in this case I need to use view rank and ignore default. Based on tests looks like it works right now but I don't understand how ES decided what value in nested field need to use for sorting if filter return a few values

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