Sort nested object in Elasticsearch Sort nested object in Elasticsearch elasticsearch elasticsearch

Sort nested object in Elasticsearch


You need to sort on the inner_hits to sort the nested objects. This will give you the desired output

GET my_index/_search{  "query": {    "nested": {      "path": "comments",      "query": {        "match_all": {}      },      "inner_hits": {        "sort": {          "comments.date": {            "order": "asc"          }        },        "size": 5      }    }  },  "_source": [    "title"  ]}

I am using source filtering to get only "title" as comments will be retrieved inside inner_hit but you can avoid that if you want

size is 5 because default value is 3 and we have 4 objects in the given example.

Hope this helps!