# Count arrays as an object

**URL:** <https://discuss.elastic.co/t/count-arrays-as-an-object/336626>\
**Category:** Elasticsearch\
**Created:** [June 21, 2023, 8:34pm UTC](https://discuss.elastic.co/t/count-arrays-as-an-object/336626 "2023-06-21T20:34:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elastic\_dude](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@elastic\_dude](https://discuss.elastic.co/u/elastic_dude)\
**Post date:** [June 21, 2023, 8:34pm UTC](https://discuss.elastic.co/t/count-arrays-as-an-object/336626/1 "2023-06-21T20:34:59Z")

</div>

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:

```json
{
    "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:

```json
[
    {
        "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:

```json
[
    {
        "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!

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [June 23, 2023, 11:57am UTC](https://discuss.elastic.co/t/count-arrays-as-an-object/336626/2 "2023-06-23T11:57:56Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html) with `cardinality` off the top of my head.

---

<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:** [July 21, 2023, 11:58am UTC](https://discuss.elastic.co/t/count-arrays-as-an-object/336626/3 "2023-07-21T11:58:23Z")

</div>

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