# Nested Object Cardinality Aggregation not Working

**URL:** <https://discuss.elastic.co/t/nested-object-cardinality-aggregation-not-working/251400>\
**Category:** Elasticsearch\
**Created:** [October 8, 2020, 8:49am UTC](https://discuss.elastic.co/t/nested-object-cardinality-aggregation-not-working/251400 "2020-10-08T08:49:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hetvakil](https://avatars.discourse-cdn.com/v4/letter/h/6f9a4e/32.png) [@hetvakil](https://discuss.elastic.co/u/hetvakil)\
**Post date:** [October 8, 2020, 8:49am UTC](https://discuss.elastic.co/t/nested-object-cardinality-aggregation-not-working/251400/1 "2020-10-08T08:49:11Z")

</div>

I want to get Total Unique email count in a Nested object. Below is the example of the document. When we keep Nested object as property in mapping then the count for cardinality is always showing zero while if that object does not have nested property then it does show unique count with same query. We would like to get unique overall count for nested object field. Below is query for aggregation. For Below data value should be 5. Any help would be appreciated to achieve this.

Sample Document Data

```auto
    {
       {
          "ID":"1",
          "Email":[
             {
                "EMAIL":"value1"
             },
             {
                "EMAIL":"value2"
             },
             {
                "EMAIL":"value1"
             },
             {
                "EMAIL":"value2"
             }
          ]
       },
       {
          "ID":"2",
          "Email":[
             {
                "EMAIL":"value3"
             },
             {
                "EMAIL":"value4"
             },
             {
                "EMAIL":"value5"
             },
             {
                "EMAIL":"value3"
             }
          ]
       }
    }

```

Aggregation Query

```auto
    {
        "size": 0,
        "aggs": {
            "count": {
                "cardinality": {
                    "field": "Email.EMAIL.keyword"
                  
                }
            }
        }
    }

```

Mapping Query

```auto
    {
       "mappings":{
          "properties":{
             "ID":{
                "type":"text",
                "fields":{
                   "keyword":{
                      "type":"keyword"
                   }
                }
             },
             "Email":{
                "type":"nested",
                "properties":{
                   "EMAIL":{
                      "type":"text",
                      "fields":{
                         "keyword":{
                            "type":"keyword"
                         }
                      }
                   }
                }
             }
          }
       }
    }

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 8, 2020, 1:49pm UTC](https://discuss.elastic.co/t/nested-object-cardinality-aggregation-not-working/251400/2 "2020-10-08T13:49:11Z")

</div>

if you use the `nested` type in the mapping, also take a look at the nested aggregation.

---

<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:** [November 5, 2020, 1:49pm UTC](https://discuss.elastic.co/t/nested-object-cardinality-aggregation-not-working/251400/3 "2020-11-05T13:49:16Z")

</div>

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