# Term Aggregation over Nested Documents - SOLVED, user error :-)

**URL:** <https://discuss.elastic.co/t/term-aggregation-over-nested-documents-solved-user-error/51128>\
**Category:** Elasticsearch\
**Created:** [May 27, 2016, 8:22am UTC](https://discuss.elastic.co/t/term-aggregation-over-nested-documents-solved-user-error/51128 "2016-05-27T08:22:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matzie](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@matzie](https://discuss.elastic.co/u/matzie)\
**Post date:** [May 27, 2016, 8:22am UTC](https://discuss.elastic.co/t/term-aggregation-over-nested-documents-solved-user-error/51128/1 "2016-05-27T08:22:33Z")

</div>

I have data that looks like this:

```
POST /test/testdoc
{
  "path": "/matt/matt-pics",
  "file": "dog.jpg",
  "mime_type": "image/jpeg",
  "META": [
    {
      "scheme_name": "Pseudo_EXIF_Scheme",
      "scheme_data": [
        {
          "value": "Nikon",
          "column_name": "CAMERA_MANUFACTURER"
        },
        {
          "value": "D80",
          "column_name": "CAMERA_MODEL"
        }
       
      ]
    }
  ]
 
}

```

and similar examples with different scheme\_name and column\_name etc.

I want to write term / bucket aggregations showing the number of top-level documents in (ideally) each permutation of scheme , column name and value.

More details at [http://pastebin.com/WNRs1P6T](http://pastebin.com/WNRs1P6T)

I've also tried flattening the structure a bit, i.e.

```
META": [
    {
      "scheme_name": "Pseudo_MP3_Scheme",
      "column_name": "ARTIST",
      "value": "foo" } ] ...

```

I've just pulled an all-nighter trying to get this to work so apologies if this isn't clear enough. Would really appreciate any help! TIA.

* * *

UPDATE - finally figured out what was wrong!

I didn't want the contents of 'scheme\_name' and 'column\_name' to show up in search results. So in my mapping (the full one, not the abbreviated one in the pastebin) I had "index": "no" set for them. Removing that and everything started to work - I used this Stack Overflow qn as a guide - [http://stackoverflow.com/questions/24800545/how-to-aggregate-over-dynamic-fields-in-elasticsearch](http://stackoverflow.com/questions/24800545/how-to-aggregate-over-dynamic-fields-in-elasticsearch)

Currently I get each word in the 'value' for each metadata item as a separate bucket, which isn't what I want, but the solution to that is, at indexing time, to also index that field as a 'raw' version, as described here: [http://stackoverflow.com/questions/24640117/elasticsearch-aggregation-returns-terms-in-key-but-not-the-complete-field-h](http://stackoverflow.com/questions/24640117/elasticsearch-aggregation-returns-terms-in-key-but-not-the-complete-field-h)

Something else that was helpful along this journey (I originally used dynamic field names) was this article: [https://www.elastic.co/blog/found-beginner-troubleshooting#keyvalue-woes](https://www.elastic.co/blog/found-beginner-troubleshooting#keyvalue-woes)

Thanks

Matt

---

<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:** [July 5, 2017, 10:48pm UTC](https://discuss.elastic.co/t/term-aggregation-over-nested-documents-solved-user-error/51128/2 "2017-07-05T22:48:32Z")

</div>


