# Nested aggregation

**URL:** <https://discuss.elastic.co/t/nested-aggregation/96781>\
**Category:** Elasticsearch\
**Created:** [August 11, 2017, 1:23pm UTC](https://discuss.elastic.co/t/nested-aggregation/96781 "2017-08-11T13:23:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KeninHouston](https://avatars.discourse-cdn.com/v4/letter/k/54ee81/32.png) [@KeninHouston](https://discuss.elastic.co/u/KeninHouston)\
**Post date:** [August 11, 2017, 1:23pm UTC](https://discuss.elastic.co/t/nested-aggregation/96781/1 "2017-08-11T13:23:37Z")

</div>

Hi all,

I'm new to ElasticSearch and I'm trying to build a query based on the following structure:

Well 1 {  
Sections {  
bottom\_depth 1  
other field 1  
}  
{  
bottom\_depth 2  
}

Well 2 {  
Sections {  
bottom\_depth 1  
other field 1  
}  
{  
bottom\_depth 2  
}

What I want to do is for each well, get the maximum ie. largest bottom\_depth for that well.

I've sort of got this code, I think its close, but its not quite there:

curl -XGET [http://localhost:9200/indexname/well/\_search?pretty](http://localhost:9200/indexname/well/_search?pretty) -d'  
{  
"query": { "bool": { "must": [{"match\_all": {}}] } },  
"aggs" : { "well\_groups": { "terms": { "field": "Guid", "size": 0 },  
"aggs": { "total\_depth" : { "max" : { "field" : "Section:bottom\_depth" } } }  
}  
}  
}'

Any ideas welcomed

---

<div class="post-metadata">

**Author:** ![Neil\_Tw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neil_tw/32/13484_2.png) [@Neil\_Tw](https://discuss.elastic.co/u/Neil_Tw)\
**Post date:** [August 14, 2017, 3:03am UTC](https://discuss.elastic.co/t/nested-aggregation/96781/2 "2017-08-14T03:03:35Z")

</div>

Hi KeninHouston  
You can refer to the concerning information website.  
😄  
[https://stackoverflow.com/questions/31052204/elasticsearch-group-and-aggregate-nested-valueshttps://stackoverflow.com/questions/31052204/elasticsearch-group-and-aggregate-nested-values](https://stackoverflow.com/questions/31052204/elasticsearch-group-and-aggregate-nested-valueshttps://stackoverflow.com/questions/31052204/elasticsearch-group-and-aggregate-nested-values)

---

<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:** [September 11, 2017, 3:03am UTC](https://discuss.elastic.co/t/nested-aggregation/96781/3 "2017-09-11T03:03:44Z")

</div>

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