title: How to aggregate over dynamic fields in elasticsearch?
body: <p>I am trying to aggregate over dynamic fields (different for different documents) via elasticsearch. Documents are like following:</p>
  
  <pre><code>[{
     "name": "galaxy note",
     "price": 123,
     "attributes": {
        "type": "phone",
        "weight": "140gm"
     }
  },{
     "name": "shirt",
     "price": 123,
     "attributes": {
        "type": "clothing",
        "size": "m"
     }
  }]
  </code></pre>
  
  <p>As you can see attributes change across documents. What Im trying to achieve is to aggregate fields of these attributes, like so:</p>
  
  <pre><code>{
       aggregations: {
           types: {
               buckets: [{key: 'phone', count: 123}, {key: 'clothing', count: 12}]
           }
       }
  }
  </code></pre>
  
  <p>I am trying <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html" rel="noreferrer">aggregation</a> feature of elasticsearch to achieve this, but not able to find correct way. Is it possible to achieve via aggregation ? Or should I start looking in to <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets.html" rel="noreferrer">facets</a>, thought it seem to be depricated.</p>