# Aggregation over nested documents

**URL:** <https://discuss.elastic.co/t/aggregation-over-nested-documents/27810>\
**Category:** Elasticsearch\
**Created:** [August 21, 2015, 12:28am UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810 "2015-08-21T00:28:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaikit](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jaikit](https://discuss.elastic.co/u/jaikit)\
**Post date:** [August 21, 2015, 12:28am UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/1 "2015-08-21T00:28:30Z")

</div>

Folks,

First all great shout out to Elastic community and team for designing and building clean product. Great job!

I am running a query to generate aggregations on nested documents. I have created the gist of all the curl calls (index creation, mapping and query. ) As you see in the mapping and my query, I am trying to generate aggregations on item name. In this case aggregations are generated over query results. That said I see aggregation of other nested documents as well in final result.

Gist link:

> <https://gist.github.com/jaikit/d77144383e53abdcd921>

For e.g  
The aggregation result is:  
"aggregations": {  
"resellers": {  
"doc\_count": 5,  
"min\_price": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "Polo Ralph Lauren Classic-Fit Mesh Polo",  
"doc\_count": 3  
},  
{  
"key": "Tommy Hilfiger Classic-Fit Ivy Polo",  
"doc\_count": 2  
}  
]  
}  
}  
}

However I was expecting the result to be only sub documents which matched the query "Tommy Hilfiger Classic-Fit Ivy Polo"

```
"aggregations": {
    "resellers": {
        "doc_count": 5,
        "min_price": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Tommy Hilfiger Classic-Fit Ivy Polo",
                    "doc_count": 2
                }
            ]
        }
    }
}

```

Appreciate any help or pointers.

---

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [August 21, 2015, 10:31am UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/2 "2015-08-21T10:31:42Z")

</div>

You need to apply a [filter aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html):

```
{
    "query": {
        "nested": {
            "path": "item",
            "query": {
                "query_string": {
                    "fields": [
                        "item.name"
                    ],
                    "query": "\"Tommy Hilfiger Classic-Fit Ivy Polo\""
                }
            }
        }
    },
    "aggs": {
        "resellers_path": {
            "nested": {
                "path": "item"
            },
            "aggs": {
                "resellers_filter": {
                    "filter": {
                        "term": {
                            "item.name.raw": "Tommy Hilfiger Classic-Fit Ivy Polo"
                        }
                    },
                    "aggs": {
                        "rawItem": {
                            "terms": {
                                "field": "item.name.raw"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![jaikit](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jaikit](https://discuss.elastic.co/u/jaikit)\
**Post date:** [August 21, 2015, 5:21pm UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/3 "2015-08-21T17:21:27Z")

</div>

Hi Dan,

Thanks for your reply. Will it work for "query\_string" type of query as well ?  
For e.g here my query includes 2 fields (name and price) and I would like to show aggregation based on those sub documents only ?  
"query": "item.name:Tommy AND item.price:30"

So in this case the result of aggregation should be just 1 document

---

<div class="post-metadata">

**Author:** ![jaikit](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jaikit](https://discuss.elastic.co/u/jaikit)\
**Post date:** [August 21, 2015, 5:22pm UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/4 "2015-08-21T17:22:40Z")

</div>

complete query:

{  
"query": {  
"nested": {  
"path": "item",  
"query": {  
"query\_string": {  
"query": "item.name:Tommy AND item.price:30"  
}  
}  
}  
},  
"aggs": {  
"resellers\_path": {  
"nested": {  
"path": "item"  
},  
"aggs": {  
"resellers\_filter": {  
"filter": {  
"term": {  
"item.name.raw": "Tommy Hilfiger Classic-Fit Ivy Polo"  
}  
},  
"aggs": {  
"rawItem": {  
"terms": {  
"field": "item.name.raw"  
}  
}  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [August 22, 2015, 8:25am UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/5 "2015-08-22T08:25:52Z")

</div>

Yes, query string will work too,

```
{
    "query": {
        "nested": {
            "path": "item",
            "query": {
                "query_string": {
                    "query": "item.name:Tommy AND item.price:30"
                }
            }
        }
    },
    "aggs": {
        "resellers_path": {
            "nested": {
                "path": "item"
            },
            "aggs": {
                "resellers_filter": {
                    "filter": {
                        "query": {
                            "query_string": {
                                "query": "item.name:Tommy AND item.price:30"
                            }
                        }
                    },
                    "aggs": {
                        "rawItem": {
                            "terms": {
                                "field": "item.name.raw"
                            }
                        }
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![jaikit](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jaikit](https://discuss.elastic.co/u/jaikit)\
**Post date:** [August 22, 2015, 6:03pm UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/6 "2015-08-22T18:03:20Z")

</div>

Super! Thanks Dan, that works for my use case.

Jaikit

---

<div class="post-metadata">

**Author:** ![JonathanAaron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathanaaron/32/24405_2.png) [@JonathanAaron](https://discuss.elastic.co/u/JonathanAaron)\
**Post date:** [October 24, 2016, 6:14pm UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/7 "2016-10-24T18:14:34Z")

</div>

This worked well for me! The documentation needs to specify how to display the data of a nested object easily. I had to piece together a lot of docs to get to this solution. Thanks Dan!

---

<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 5, 2017, 10:10pm UTC](https://discuss.elastic.co/t/aggregation-over-nested-documents/27810/8 "2017-07-05T22:10:10Z")

</div>


