# Hierarchical aggregations

**URL:** https://discuss.elastic.co/t/hierarchical-aggregations/50137
**Category:** Elasticsearch
**Created:** [May 16, 2016, 4:21pm UTC](https://discuss.elastic.co/t/hierarchical-aggregations/50137 "2016-05-16T16:21:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Geoff\_Stockham](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@Geoff\_Stockham](https://discuss.elastic.co/u/Geoff_Stockham)
#### Post date: [May 16, 2016, 4:21pm UTC](https://discuss.elastic.co/t/hierarchical-aggregations/50137/1 "2016-05-16T16:21:04Z")

</div>

Hi,

I think I'm getting a bit lost in the terminology for aggregations (nested, sub-, reverse-nested etc.) and am struggling to construct a query to return what I want from an aggregation.

I have some hierarchical metadata on an item, and an item can belong to multiple categories (e.g. Amazon book catagories). I'm currently constructing the item to have a single top-level item for each top-level category, which has children.

e.g.

```
Item 1
- Categories
     - Category 1
            - Sub Category 1_1
            - Sub Category 1_2
     - Category 2
            - Sub Category 2_1

```

I would like to return a nested / sub / hierarchical aggregation where the top-level aggregation returns only the top-level categories, and below that it returns **only the subcategories that belong to that top-level category**.

However, at the moment, my query returns **all** second level categories under each top-level aggregation.

I can't work out if my issue is in how I map / index the document, or whether I am constructing the query incorrectly (e.g. can I add a filter based on the parent aggregation somehow?).

I've created a gist that tries to recreate the issue (it's just a list of Sense commands), and would be grateful of any pointers in the right direction!

> <https://gist.github.com/gjstockham/bdd47c17a44afdd59968d0d5c306482e>

Thanks

Geoff

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [May 17, 2016, 2:07pm UTC](https://discuss.elastic.co/t/hierarchical-aggregations/50137/2 "2016-05-17T14:07:41Z")

</div>

Hi Geoff,  
Try this:

> <https://gist.github.com/markharwood/86246ef2983b286611f4c4439dee5e99>

---

<div class="post-metadata">

### Author: ![Geoff\_Stockham](https://avatars.discourse-cdn.com/v4/letter/g/4af34b/32.png) [@Geoff\_Stockham](https://discuss.elastic.co/u/Geoff_Stockham)
#### Post date: [May 17, 2016, 3:01pm UTC](https://discuss.elastic.co/t/hierarchical-aggregations/50137/3 "2016-05-17T15:01:59Z")

</div>

Mark, you are a genius 🙂

Thanks very much!

---

<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:50pm UTC](https://discuss.elastic.co/t/hierarchical-aggregations/50137/4 "2017-07-05T22:50:59Z")

</div>


