# Nested objects aggregation

**URL:** <https://discuss.elastic.co/t/nested-objects-aggregation/142635>\
**Category:** Elasticsearch\
**Created:** [August 1, 2018, 5:38pm UTC](https://discuss.elastic.co/t/nested-objects-aggregation/142635 "2018-08-01T17:38:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashwin.srik](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@ashwin.srik](https://discuss.elastic.co/u/ashwin.srik)\
**Post date:** [August 1, 2018, 5:38pm UTC](https://discuss.elastic.co/t/nested-objects-aggregation/142635/1 "2018-08-01T17:38:56Z")

</div>

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",
                                       }
                         ]
           }
}
]
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 29, 2018, 5:39pm UTC](https://discuss.elastic.co/t/nested-objects-aggregation/142635/2 "2018-08-29T17:39:16Z")

</div>

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