# Nested list aggregation

**URL:** <https://discuss.elastic.co/t/nested-list-aggregation/22854>\
**Category:** Elasticsearch\
**Created:** [March 24, 2015, 12:26pm UTC](https://discuss.elastic.co/t/nested-list-aggregation/22854 "2015-03-24T12:26:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vasily\_Kirichenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasily_kirichenko/32/806_2.png) [@Vasily\_Kirichenko](https://discuss.elastic.co/u/Vasily_Kirichenko)\
**Post date:** [March 24, 2015, 12:26pm UTC](https://discuss.elastic.co/t/nested-list-aggregation/22854/1 "2015-03-24T12:26:29Z")

</div>

I have documents like this in my index:

{  
"time": "2015-03-24T08:24:55.9056988",  
"msg": {  
"corrupted": false,  
"stat": {  
"fileSize": 10186,  
"stages": [  
{  
"stage": "queued",  
"duration": 420  
},  
{  
"stage": "validate",  
"duration": 27  
},  
{  
"stage": "cacheFile",  
"duration": 87  
},  
{  
"stage": "sendResult",  
"duration": 1332  
}  
]  
}  
}  
}

I'd like to calculate sum(msg.stat.stages.duration) grouped by  
msg.stat.stages.stage.  
I tried the following:

{  
"size": 0,  
"aggs":  
{  
"1":  
{  
"terms": { "field": "msg.stat.stages.stage" },  
"aggs":  
{  
"2":  
{  
"nested": { "path": "stat.stages" },  
"aggs":  
{  
"3": {  
"sum": {  
"field": "stat.stages.duration"  
}  
}  
}  
}  
}  
}  
},  
"query": {  
"match\_all": {}  
}  
}

and got:

{  
"took": 6,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 0,  
"hits": []  
},  
"aggregations": {  
"1": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"2": {  
"doc\_count": 0  
},  
"key": "cachefile",  
"doc\_count": 1  
},  
{  
"2": {  
"doc\_count": 0  
},  
"key": "queued",  
"doc\_count": 1  
},  
{  
"2": {  
"doc\_count": 0  
},  
"key": "sendresult",  
"doc\_count": 1  
},  
{  
"2": {  
"doc\_count": 0  
},  
"key": "validate",  
"doc\_count": 1  
}  
]  
}  
}  
}

which is not what I expected. Any ideas?

Thanks!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/22173f85-6d94-4b29-a9f5-b13c46a4850d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/22173f85-6d94-4b29-a9f5-b13c46a4850d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![masaru](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@masaru](https://discuss.elastic.co/u/masaru)\
**Post date:** [March 25, 2015, 8:17am UTC](https://discuss.elastic.co/t/nested-list-aggregation/22854/2 "2015-03-25T08:17:52Z")

</div>

Hi,

If you define "stages" as nested type, query like this should work:

{  
"query": {  
"match\_all": {}  
},  
"aggs": {  
"0": {  
"nested": {  
"path": "msg.stat.stages"  
},  
"aggs": {  
"1": {  
"terms": {  
"field": "stage"  
},  
"aggs": {  
"2": {  
"sum": {  
"field": "duration"  
}  
}  
}  
}  
}  
}  
}  
}

Masaru

On March 24, 2015 at 21:26:33, Vasily Kirichenko ([vaskir@gmail.com](mailto:vaskir@gmail.com)) wrote:

I have documents like this in my index:

{  
"time": "2015-03-24T08:24:55.9056988",  
"msg": {  
"corrupted": false,  
"stat": {  
"fileSize": 10186,  
"stages": [  
{  
"stage": "queued",  
"duration": 420  
},  
{  
"stage": "validate",  
"duration": 27  
},  
{  
"stage": "cacheFile",  
"duration": 87  
},  
{  
"stage": "sendResult",  
"duration": 1332  
}  
]  
}  
}  
}

I'd like to calculate sum(msg.stat.stages.duration) grouped by msg.stat.stages.stage.  
I tried the following:

{  
"size": 0,  
"aggs":  
{  
"1":  
{  
"terms": { "field": "msg.stat.stages.stage" },  
"aggs":  
{  
"2":  
{  
"nested": { "path": "stat.stages" },  
"aggs":  
{  
"3": {  
"sum": {  
"field": "stat.stages.duration"  
}  
}  
}  
}  
}  
}  
},  
"query": {  
"match\_all": {}  
}  
}

and got:

{  
"took": 6,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 0,  
"hits": []  
},  
"aggregations": {  
"1": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"2": {  
"doc\_count": 0  
},  
"key": "cachefile",  
"doc\_count": 1  
},  
{  
"2": {  
"doc\_count": 0  
},  
"key": "queued",  
"doc\_count": 1  
},  
{  
"2": {  
"doc\_count": 0  
},  
"key": "sendresult",  
"doc\_count": 1  
},  
{  
"2": {  
"doc\_count": 0  
},  
"key": "validate",  
"doc\_count": 1  
}  
]  
}  
}  
}

which is not what I expected. Any ideas?

## Thanks!

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/22173f85-6d94-4b29-a9f5-b13c46a4850d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/22173f85-6d94-4b29-a9f5-b13c46a4850d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.55126f6c.238e1f29.166%40citra-2.local](https://groups.google.com/d/msgid/elasticsearch/etPan.55126f6c.238e1f29.166%40citra-2.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:24am UTC](https://discuss.elastic.co/t/nested-list-aggregation/22854/3 "2017-07-06T00:24:10Z")

</div>

Closed on July 6, 2017, 12:24am UTC


