# How to return subset of nested aggregation objects?

**URL:** <https://discuss.elastic.co/t/how-to-return-subset-of-nested-aggregation-objects/51732>\
**Category:** Elasticsearch\
**Created:** [June 2, 2016, 6:37pm UTC](https://discuss.elastic.co/t/how-to-return-subset-of-nested-aggregation-objects/51732 "2016-06-02T18:37:35Z")\
**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:** [June 2, 2016, 6:37pm UTC](https://discuss.elastic.co/t/how-to-return-subset-of-nested-aggregation-objects/51732/1 "2016-06-02T18:37:35Z")

</div>

Hi everyone,  
I have Product documents that have nested Attributes. Each Product document has a full set of nested Attributes, but I would like to control which Attributes get returned.

I thought I could use a QueryContainer that is a list of all Attributes that I would like to return in the aggregation, like this:

```
QueryContainer attDisplay = null;
        foreach (ESAttribute attribute in category.Attributes)
            attDisplay |= new TermQuery() { Field = "attributes.attributeId", Value = attribute.AttributeID };

```

But I'm not sure how to use it.

Here is my attribute aggregation which returns all Attributes for the Products. I am filtering the Product documents by categories and manufacturers here, but I want to be able to filter the nested "attributes" by the query above. I don't want to filter the parent Product documents, but filter the nested Attribute aggregation that gets returned. Does anyone know of a way to accomplish this?

Thanks!

```
.Aggregations(agg => agg
    .Filter("att_agg", cf => cf
        .Filter(f => f
            .Bool(b => b
                .Must(ms =>
                    ms.Nested(n => n.Path("categories").Query(q => q.Bool(b1 => b1.Must(m => catQuery || m.Strict(catQuery == null))))) &&
                    ms.Nested(n => n.Path("manufacturer").Query(q => q.Bool(b1 => b1.Must(m => manQuery || m.Strict(manQuery == null)))))
                )
            )
        )
        .Aggregations(a => a
            .Nested("att_nest", n => n
                .Path("attributes")
                .Aggregations(a1 => a1.Terms("agg_attNames", f1 => f1.Field("attributes.idName").Size(5)
                    .Aggregations(a2 => a2.Terms("agg_attValues", f2 => f2.Field("attributes.attributeValue").Size(5))))
                )
            )
        )
    )
)
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 3, 2016, 2:33pm UTC](https://discuss.elastic.co/t/how-to-return-subset-of-nested-aggregation-objects/51732/2 "2016-06-03T14:33:31Z")

</div>

These questions stand a better chance of being solved if they are expressed in the JSON syntax rather than a particular language client (I'm guessing c# here?). That way we can run them in curl or Sense without too much setup.

---

<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:46pm UTC](https://discuss.elastic.co/t/how-to-return-subset-of-nested-aggregation-objects/51732/3 "2017-07-05T22:46:24Z")

</div>


