-How to sort within nested objects

Hi All

I have a document like the following:

{
  "applicant" : {
      "firstName" : "tasd",
      "lastName" : "asdfasdf"
  },
  "employmentsHistory" : [
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2014-02-26",
      "endDate" : "2016-01-20"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2018-02-26",
      "endDate" : "2020-01-20"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2016-02-16",
      "endDate" : "2018-01-18"
    }
  ]     
}

I want to get the doc. The nested object employmentsHistory should be sorted on the endDate field when returned. i.e

Should return:

{
  "applicant" : {
      "firstName" : "tasd",
      "lastName" : "asdfasdf"
  },
  "employmentsHistory" : [
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2014-02-26",
      "endDate" : "2016-01-20"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2016-02-16",
      "endDate" : "2018-01-18"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2018-02-26",
      "endDate" : "2020-01-20"
    }
  ]     
}

I have tried multiple approaches for sorting. Seems, those sort gets applied on the parent documents. The inner hits query approach returns data on separate scope also. Will be glad if anyone helps me out? Thanks.

can you be more exact what you mean with

I want to get the doc

Are you searching for it? Are you just retrieving it? How does your search look like, can you provide an example? Also, please provide the mapping of your index plus index creation and full sample documents as part of a reproducible but minimal example, so others can reproduce your problem and understand more easily.

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