# Aggregation based off of nested fields and filtering

**URL:** <https://discuss.elastic.co/t/aggregation-based-off-of-nested-fields-and-filtering/353214>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [February 13, 2024, 10:54pm UTC](https://discuss.elastic.co/t/aggregation-based-off-of-nested-fields-and-filtering/353214 "2024-02-13T22:54:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jessiditocco](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jessiditocco/32/131703_2.png) [@jessiditocco](https://discuss.elastic.co/u/jessiditocco)\
**Post date:** [February 13, 2024, 10:54pm UTC](https://discuss.elastic.co/t/aggregation-based-off-of-nested-fields-and-filtering/353214/1 "2024-02-13T22:54:00Z")

</div>

I have an index of Provider documents that look roughly like this:

```auto

[
    {
        "slug": "provider1",
        "name": "dr. evil",
        "employer_networks": [
            {
                "slug": "network-a",
                "tier": 2,
            },
            {"slug": "network-b", "tier": null},
            {"slug": "network-c", "tier": 2},
        ],
        "formatted_name": "Dr. Evil",
    },
    {
        "slug": "provider2",
        "name": "dr. grey",
        "employer_networks": [
            {
                "slug": "network-a",
                "tier": 101,
            },
            {"slug": "network-b", "tier": 2},
            {"slug": "network-c", "tier": null},
        ],
        "formatted_name": "Meredith Grey",
    },
]

```

You'll notice that for each provider, there is an "employer\_networks" field which is a list of objects.

I want to create an aggregation that gives me a count of all of the tiers for providers where the employer networks slug is equal to "network\_a". If the employer\_networks.slug is null, or something else, don't count that provider in the count.

So the final counts result would look something like this:

```auto
counts: {
    {
        "tier": 101,
        "count": 1
        
    },
    {
        "tier": 2,
        "count": 1
        
    }
}

```

How can I create such an aggregation? I am on Elasticsearch 1.8.0.

---

<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:** [March 12, 2024, 10:54pm UTC](https://discuss.elastic.co/t/aggregation-based-off-of-nested-fields-and-filtering/353214/2 "2024-03-12T22:54:13Z")

</div>

Closed on March 12, 2024, 10:54pm UTC

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