# Filter nested field only in aggregation

**URL:** <https://discuss.elastic.co/t/filter-nested-field-only-in-aggregation/206741>\
**Category:** Elasticsearch\
**Created:** [November 6, 2019, 7:52am UTC](https://discuss.elastic.co/t/filter-nested-field-only-in-aggregation/206741 "2019-11-06T07:52:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)\
**Post date:** [November 6, 2019, 7:52am UTC](https://discuss.elastic.co/t/filter-nested-field-only-in-aggregation/206741/1 "2019-11-06T07:52:19Z")

</div>

My index has a nested field, looking like this:

```
              "services": {
                  "type": "nested",
                  "properties": {
                        "type": {
                            "type": "keyword"
                        },
                        "quantity": {
                            "type": "keyword"
                        },
                       "alias": {
                            "type": "keyword"
                        },

```

I want to have the sum of all quantity of the services group by `type` and `alias` expect for type with value as `abc` for instance.

Here is a part of the aggregation, but it has parsing\_exception:

```
            "aggregations": {
            "nested": {
                "nested": {
                    "path": "services",
                    "query": {
                    "bool": {
                        "must_not": [
                            {
                                "term": {
                                    "services.type": "abc"
                                }
                            }
                        ]
                    }
                }
                },
                
                "aggregations": {
                    "company_group": {
                        "terms": {
                            "field": "services.alias",
                            "size": 1000
                        },
                        "aggregations": {
                            "res_type": {
                                "terms": {
                                    "field": "services.type",
                                    "size": 10
                                },
                                "aggregations": {
                                    "daily_overage": {
                                        "sum": {
                                            "field": "services.quantity"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            },

```

Is there any way to filter only the nested field `service` above?

---

<div class="post-metadata">

**Author:** ![Seetharaman\_K](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seetharaman_k/32/52542_2.png) [@Seetharaman\_K](https://discuss.elastic.co/u/Seetharaman_K)\
**Post date:** [November 6, 2019, 8:46am UTC](https://discuss.elastic.co/t/filter-nested-field-only-in-aggregation/206741/2 "2019-11-06T08:46:16Z")

</div>

is it solved ?

---

<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:** [December 4, 2019, 8:46am UTC](https://discuss.elastic.co/t/filter-nested-field-only-in-aggregation/206741/3 "2019-12-04T08:46:17Z")

</div>

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