Nested objects aggregation

Is there a way to perform aggregation to get the document count based only the latest value of date and the date ranges below?

Expected Result based on today being “2018-08-01”:
Past: 1
0-1Year: 0
1-2Years: 1
3+Years: 0

We are using elasticsearch version 6.2.4 but we can upgrade to 6.3 if that can help solve the problem.

Example of the structure of the mapping:

     doc: {
         properties:{
             lv1:{
                 type: "nested",
                 properties: {
                     "lv2": {
                           "type": "nested",
                             "properties": {
                                   "date": {
                                         type: "date"
                                      }
                                }      
                         }
                }
         }
   }

Example of the document :

[
{
 "lv1": {
             "lv2": [
                                     {
                                        date: "2017-07-26T04:19:27.1783118+00:00",
                                      },
                                      {
                                         date: "2018-07-26T04:19:27.1783118+00:00",
                                       },
                                       {
                                          date: "2016-07-26T04:19:27.1783118+00:00",
                                       }
                         ]
           }
},
{
 "lv1": {
             "lv2": [
                                     {
                                        date: "2019-07-26T04:19:27.1783118+00:00",
                                      },
                                      {
                                         date: "2020-07-26T04:19:27.1783118+00:00",
                                       },
                                       {
                                          date: "2018-07-26T04:19:27.1783118+00:00",
                                       }
                         ]
           }
}
]

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