How can I sort nested fields?

73 Views Asked by At

Below data is the output of the search query of the elastic search index. I want to sort data based on review.rate field.

How can I do it?

Data

[{
    "name": "ABC",
    "review": [{
        'rate': 1.5,
        'note': "XYZ", 
        'type': "review"
    },
    {
        'rate': 4.5,
        'note': "XYZ",
        'type': "note"
    },
    {
        'rate': 2.0,
        'note': "XYZ",
        'type': "comment"
    }]
},
{
    "name": "PQR",
    "review": [{
        'rate': "",
        'note': "XYZ", 
        'type': "comment"
    },
    {
        'rate': 3.5,
        'note': "XYZ",
        'type': "note"
    },
    {
        'rate': 1.0,
        'note': "XYZ",
        'type': "review"
    }]
},
{
    "name": "STU",
    "review": [{
        'rate': 5.0,
        'note': "XYZ",
        'type': "comment" 
    },
    {
        'rate': 3.5,
        'note': "XYZ",
        'type': "review"
    },
    {
        'rate': 1.5,
        'note': "XYZ",
        'type': "note"
    }]
}]

Tried somethings as below

Query

{
    "_source": {
        "includes": [
            "name"
        ]
    },
    "query":{
        "bool": [{
          "nested": {
            "path": "review",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "review.type": "comment"
                    }
                  }
                ]
              }
            }
          }
        }]
    },
    "sort": ["review.rate"]
}

Output must be [PQR, ABC, STU]. How can I achieve that?

0

There are 0 best solutions below