# Aggregation on specific object in an array

**URL:** <https://discuss.elastic.co/t/aggregation-on-specific-object-in-an-array/327533>\
**Category:** Elasticsearch\
**Created:** [March 13, 2023, 9:07am UTC](https://discuss.elastic.co/t/aggregation-on-specific-object-in-an-array/327533 "2023-03-13T09:07:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 13, 2023, 9:07am UTC](https://discuss.elastic.co/t/aggregation-on-specific-object-in-an-array/327533/1 "2023-03-13T09:07:20Z")

</div>

Hi,  
So my document has a structure as below. I would like to aggregate based on ` **value** `, but only on the object with `{"label": "Business Priority"}`.  
Can you help how I can achieve this?  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/e/0e466e79222ebea0fb96c36bbf013e17f7d5b3ae.png)

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [March 13, 2023, 12:42pm UTC](https://discuss.elastic.co/t/aggregation-on-specific-object-in-an-array/327533/2 "2023-03-13T12:42:35Z")

</div>

Hi,

If custom\_fields is a `nested field`, use `Nested aggregation`.  
The sample with a filter sub-aggregation, in the middle of the page below, might help you.

> **[Nested aggregation | Elasticsearch Guide \[8.6\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html)**

---

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 13, 2023, 1:02pm UTC](https://discuss.elastic.co/t/aggregation-on-specific-object-in-an-array/327533/3 "2023-03-13T13:02:31Z")

</div>

Using this payload.

```auto
{
    "size": 0,
    "aggs": {
        "agg_custom_field": {
            "nested": {
                "path": "custom_fields"
            },
            "aggs": {
                "root_filter": {
                    "filter": {
                        "bool": {
                            "filter": [
                                {
                                    "term": {
                                        "custom_fields.label": "Application Module"
                                    }
                                }
                            ]
                        }
                    },
                    "aggs": {
                        "agg_value": {
                            "terms": {
                                "field": "custom_fields.value"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

Response is this  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/7/b759c7e3c1d8aaa4fd30422d6b4d7cc968550c6d.png)

---

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [March 13, 2023, 1:27pm UTC](https://discuss.elastic.co/t/aggregation-on-specific-object-in-an-array/327533/4 "2023-03-13T13:27:33Z")

</div>

This one worked 😃

```auto
{
    "size": 0,
    "aggs": {
        "agg_custom_field": {
            "nested": {
                "path": "custom_fields"
            },
            "aggs": {
                "root_filter": {
                    "filter": {
                        "bool": {
                            "filter": [
                                {
                                    "match": {
                                        "custom_fields.label": "Application Module"
                                    }
                                }
                            ]
                        }
                    },
                    "aggs": {
                        "agg_value": {
                            "terms": {
                                "field": "custom_fields.value"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

---

<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 10, 2023, 1:28pm UTC](https://discuss.elastic.co/t/aggregation-on-specific-object-in-an-array/327533/5 "2023-04-10T13:28:15Z")

</div>

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