Elastic Search custom Sorting

I have a special scenario for sorting and I am not aware how to achieve that using ES Nest library.
Here is how my data looks like :

[
    {
        "id": "1",
        "overallSpeedScore": 68,
        "name": abc,
        "tradeScores": [
            {
                "tradeName": "HVAC",
                "speedScore": 95,
                "price": 45.75
            },
            {
                "tradeName": "Electric",
                "speedScore": 94,
                "price": 45.75
            }
        ]
    },
    {     
        "id": "2",
        "overallSpeedScore": 70,
        "price": 5.95,
        "tradeScores": []
     },
     {
        "id": "3",
        "overallSpeedScore": 92,
        "price": 5.95,
        "tradeScores": [
            {
                "tradeName": "HVAC",
                "speedScore": 85,
                "price": 45.75
            }
         ]
    },
    {     
        "id": "4",
        "overallSpeedScore": 82,
        "price": 5.95,
        "tradeScores": []
     },
     {     
        "id": "5",
        "overallSpeedScore": 88,
        "price": 5.95,
        "tradeScores": []
     }
]

There are 2 conditions for sort ( tradeName is user input and I am using HVAC tradename as an example here):

  1. If I am sorting with tradename = "HVAC", it should first sort Desc all the records on tradescores.speedscore where tradeScores.tradeName = "HVAC".
  2. For all the records where tradeScores.tradeName = "HVAC" is not available or where tradeScores is Null, it should sort desc on overallSpeedScore field.

Results of first sort should be ranked higher and display before all the results of second sort.
So here it should be sorted in this order : id 1,3, 5,4,2

I am using SortDescriptor and can apply first condition successfully. But I don't know how to add second condition for sorting.

Can someone help me here ? Thank you so much!

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