Hierarchical aggregations


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.


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!



Hi Geoff,
Try this:

1 Like

Mark, you are a genius :slight_smile:

Thanks very much!

1 Like