Elasticsearch array value count aggregation
<p><strong>Sample documents</strong>:</p>

<pre><code>{
    "id": "62655",
    "attributes": [
        {
            "name": "genre",
            "value": "comedy"
        },
        {
            "name": "year",
            "value": "2016"
        }
    ]
}

{
    "id": "62656",
    "attributes": [
        {
            "name": "genre",
            "value": "horror"
        },
        {
            "name": "year",
            "value": "2016"
        }
    ]
}

{
    "id": "62657",
    "attributes": [
        {
            "name": "language",
            "value": "english"
        },
        {
            "name": "year",
            "value": "2015"
        }
    ]
}
</code></pre>

<p><strong>Expected Output</strong>:</p>

<pre><code>{
    "hits" : {
        "total": 3,
        "hits": []
    },
    "aggregations": {
        "attribCount": {
            "language": 1,
            "genre": 2,
            "year": 3
        },
        "attribVals": {
            "language": {
                "english": 1
            },
            "genre": {
                "comedy": 1,
                "horror": 1
            },
            "year": {
                "2016": 2,
                "2015": 1
            }
        }
    }
}
</code></pre>

<p><strong>My Query</strong>:</p>

<p>I could get the "<em>attribCount</em>" aggregation using below query. But I don't know how to get each attribute value count.</p>

<pre><code>{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            }
        }
    },
    "aggs": {
        "attribCount": {
            "terms": {
                "field": "attributes.name",
                "size": 0
            }
        }
    },
    "size": 0
}
</code></pre>

<p>When I aggregate using attributes.value, it gives overall count. But I need it listed under the name value as given in expected output.</p>