# Nested Aggregation (Group by cluase)

**URL:** <https://discuss.elastic.co/t/nested-aggregation-group-by-cluase/18191>\
**Category:** Elasticsearch\
**Created:** [June 18, 2014, 5:39pm UTC](https://discuss.elastic.co/t/nested-aggregation-group-by-cluase/18191 "2014-06-18T17:39:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayushsangani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ayushsangani/32/9612_2.png) [@ayushsangani](https://discuss.elastic.co/u/ayushsangani)\
**Post date:** [June 18, 2014, 5:39pm UTC](https://discuss.elastic.co/t/nested-aggregation-group-by-cluase/18191/1 "2014-06-18T17:39:39Z")

</div>

Hi All,

I am pretty new to elasticsearch, my project requires me to write a  
group-by clause query.  
I am trying to write a nested dsl using aggregation which will be like a  
group by clause.

Structure of Input Document is something like this:

{  
"\_source": {  
"id": 1234,  
"oid": 6,

```
           "education": [
              {
                 "school_name": "Harvard",
                 "city" : "Boston",
                 "year": 1965,
                 "degree": "Undergrad"
              },
              {
                 "school_name": "Harvard",
                 "city" : "Boston",
                 "year": 1975,
                 "degree": "Masters"
              },
              {
                 "school_name": "Harvard",
                 "city" : "Boston",
                 "year": 1958,
                 "degree": "BA"
              }  
           ],
        }
     },

```

----Another records... and so on

\*Above shown document complies to one record.

Goal: I am trying to find out all those students who studied in Boston.  
So Ideally if I have only above document then I should get only 1 record.

With the nested aggregation query I have written below I am getting 3 as a  
count for Boston

GET cluster\_test/index\_test/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"oid": {  
"value": "6"  
}  
}  
}  
]  
}  
},  
"aggs": {  
"education": {  
"nested": {  
"path": "education"  
},  
"aggs": {  
"edu": {  
"terms": {  
"field": "education.city",  
"size": 0  
}  
}  
}  
}  
}  
}

If anyone can point out where I am going wrong or what is better to deal  
with these type of queries.  
Any help is appreciated.

--  
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/4dc2ccc6-114e-4594-8a49-b19d4c41742f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4dc2ccc6-114e-4594-8a49-b19d4c41742f%40googlegroups.com).  
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, 1:21am UTC](https://discuss.elastic.co/t/nested-aggregation-group-by-cluase/18191/2 "2017-07-06T01:21:28Z")

</div>


