# Aggregation on nested objects

**URL:** https://discuss.elastic.co/t/aggregation-on-nested-objects/46731
**Category:** Elasticsearch
**Created:** [April 7, 2016, 7:12pm UTC](https://discuss.elastic.co/t/aggregation-on-nested-objects/46731 "2016-04-07T19:12:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![primico](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@primico](https://discuss.elastic.co/u/primico)
#### Post date: [April 7, 2016, 7:12pm UTC](https://discuss.elastic.co/t/aggregation-on-nested-objects/46731/1 "2016-04-07T19:12:29Z")

</div>

Hi,  
I have product documents that have nested categories, as shown in the json below. When I aggregate categories, I would only like to aggregate the ones in the current category tree. For example, if I was currently in category 294, I would like it to aggregate Category1, but ignore Category2. My code below aggregates all categories in the category node. Any idea how I would filter what the aggregation is analyzing?  
Thanks!

```
 .Aggregations(a => a
     .Nested("cat_nest", n => n
         .Path("categories")
         .Aggregations(a1 => a1.Terms("agg_categories", t => t.Field("categories.idName")))
     )
 )

{
      "_index": "myindex",
      "_type": "product",
      "_id": "7308",
      "_version": 1,
      "found": true,
      "_source": {
        "manufacturerId": 260,
        "categories": [
          {
            "id": 294,
            "parentCategoryId": 2794,
            "name": "Category1",
            "idName": "294|Category1",
            "categoryPath": "/1/2794/294"
          },
          {
            "id": 1710,
            "parentCategoryId": 63,
            "name": "Category2",
            "idName": "1710|Category2",
            "categoryPath": "/63/1710"
          }
        ],
        "categoryPaths": ["/1/2794/294", "/63/1710"],
        "id": 7308,
        "name": "Product Name",
        "sku": "340002"
      }
}
```

---

<div class="post-metadata">

### Author: ![primico](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@primico](https://discuss.elastic.co/u/primico)
#### Post date: [April 8, 2016, 1:45am UTC](https://discuss.elastic.co/t/aggregation-on-nested-objects/46731/2 "2016-04-08T01:45:07Z")

</div>

I did a nested filtered aggregation and now I get the results I need. In case anyone else is wondering, this is how I did it:

```
                .Aggregations(a => a
                    .Nested("cat_nest", n => n
                        .Path("categories")
                        .Aggregations(a1 => a1
                            .Filter("filter", f => f
                                .Filter(f1 => f1.Term(t => t.Field("categories.categoryPath").Value(current.RootCategory.CategoryPath)))
                                .Aggregations(a2 => a2.Terms("agg_categories", t => t.Field("categories.idName")))
                            )
                        )
                    )
                )
```

---

<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, 11:01pm UTC](https://discuss.elastic.co/t/aggregation-on-nested-objects/46731/3 "2017-07-05T23:01:19Z")

</div>


