Aggregation on specific object in an array

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

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.

Using this payload.

{
    "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

This one worked :smiley:

{
    "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"
                            }
                        }
                    }
                }
            }
        }
    }
}
1 Like

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