# Creating a pie with aggregation and sub-aggregation on the same field

**URL:** <https://discuss.elastic.co/t/creating-a-pie-with-aggregation-and-sub-aggregation-on-the-same-field/125511>\
**Category:** Kibana\
**Created:** [March 25, 2018, 8:15pm UTC](https://discuss.elastic.co/t/creating-a-pie-with-aggregation-and-sub-aggregation-on-the-same-field/125511 "2018-03-25T20:15:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![m3000](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@m3000](https://discuss.elastic.co/u/m3000)\
**Post date:** [March 25, 2018, 8:15pm UTC](https://discuss.elastic.co/t/creating-a-pie-with-aggregation-and-sub-aggregation-on-the-same-field/125511/1 "2018-03-25T20:15:35Z")

</div>

Hi,  
I have an index containing documents representing a products. Each product includes a field called "category" with multiple values.  
I would like to display a pie chart splited by each category, and also display "sub-categories" using sub aggregation on the same field. Is there a way to remove the "main" category from the sub aggregation?  
for example:  
If I have 3 categories {A, B, C}:  
sub aggregation on bucket A should include only {B, C}  
sub aggregation on bucket B should include only {A, C}  
sub aggregation on bucket C should include only {A, B}

Thanks.

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [March 26, 2018, 2:29pm UTC](https://discuss.elastic.co/t/creating-a-pie-with-aggregation-and-sub-aggregation-on-the-same-field/125511/2 "2018-03-26T14:29:13Z")

</div>

Are you saying that the `category` field contains either a category or a sub-category? If so, I don't think you're going to get good results with that data.

You could try using the `Filters` aggregation type where you would create a filter for each product in the first aggregation, and then `Add sub-bucket` and `Split slices` and use another `Filters` aggregation for the sub-category. Something like this;

![image](https://us1.discourse-cdn.com/elastic/original/3X/f/1/f1a934a05f6fbb69a44e0f15b77d9e1f09c1a0b0.png)

But I think this would only work properly if category and sub-category were two separate fields and each doc in your index had both those fields set properly.

It might be possible to use 2 scripted fields to create a new category and sub-category field but I think you're have to script the name of every category and sub-category which might not scale very well.

Is there something that indicates if it's a category or sub-category?

Regards,  
Lee

---

<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:** [April 23, 2018, 2:29pm UTC](https://discuss.elastic.co/t/creating-a-pie-with-aggregation-and-sub-aggregation-on-the-same-field/125511/3 "2018-04-23T14:29:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
