Aggregation based off of nested fields and filtering

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


[
    {
        "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:

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

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

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