Count arrays as an object

Hi, I am struggling to get an aggregation to work. We have an array with multiple values and what I am trying to do is count (and return the values) how many documents have the exact same set of values in the array.

The below is a (truncated) mapping example:

{
    "mappings": {
        "properties": {
            "parentField": {
                "type": "nested",
                "properties": {
                    "childField": {
                        "type": "keyword"
                    },
                    "childArray": {
                        "type": "nested",
                        "properties": {
                            "childArrayField": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

The data might look something like these two example documents:

[
    {
        "parentField": {
            "childField": "childFieldValueOne",
            "childArray": [
                {
                    "childArrayField": "childArrayFieldValueOne"
                },
                {
                    "childArrayField": "childArrayFieldValueTwo"
                }
            ]
        }
    },
    {
        "parentField": {
            "childField": "childFieldValueTwo",
            "childArray": [
                {
                    "childArrayField": "childArrayFieldValueThree"
                }
            ]
        }
    }    
]

For this simplified example I would like get output that basically states something like:

[
    {
        "childArrayValue": [
            {
                "childArrayField": "childArrayFieldValueOne"
            },
            {
                "childArrayField": "childArrayFieldValueTwo"
            }
        ],
        "count": 1
    },
    {
        "childArrayValue": [
            {
                "childArrayField": "childArrayFieldValueThree"
            }
        ],
        "count": 1
    }   
]

Obviously the example is very basic and would be much larger in reality but hopefully it conveys the idea.

Maybe I do not have the 'nested' designator in the correct place? I have tried 'terms', 'composite', and 'cardinality' aggregations with little luck outside of being able to count the distinct 'childArrayField' occurrences but I would like to count the 'childArray' combination occurrences.

Help is much appreciated!

Hi @elastic_dude,

Can you share the query that you are running? I'm aware you have tried multiple options, but it sounds like you want to use a nested aggregation with cardinality off the top of my head.

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