# Using Nested Fields in Composite Aggregation

**URL:** https://discuss.elastic.co/t/using-nested-fields-in-composite-aggregation/241921
**Category:** Elasticsearch
**Created:** [July 20, 2020, 6:03pm UTC](https://discuss.elastic.co/t/using-nested-fields-in-composite-aggregation/241921 "2020-07-20T18:03:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lmariselvam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lmariselvam/32/54542_2.png) [@lmariselvam](https://discuss.elastic.co/u/lmariselvam)
#### Post date: [July 20, 2020, 6:03pm UTC](https://discuss.elastic.co/t/using-nested-fields-in-composite-aggregation/241921/1 "2020-07-20T18:03:34Z")

</div>

Basically, I'm trying to find the duplicate contacts by first name, last name & email address. For that, I've tried to use composite aggregation with the fields firstName, lastName & emails.email, the response from the query has the values bucketed for non-nested fields(such as firstName & lastName), but the nested field emails.email doesn't have value at all -\> it returns NULL: [https://www.screencast.com/t/98CKr0I5](https://www.screencast.com/t/98CKr0I5)

Am I missing something here? any help would be greatly appreciated.

Below is one of the example document

```auto
    {
    "regionId": 10,
    "firstName": "John",
    "lastName": "mayer",
    "emails": [
      {
        "isPrimary": true,
        "email": "sample@gmail.com"
      }
    ]
}

```

And, I'm trying to query the Elasticsearch as follows:

```auto
    GET contacts/_search
{
  "size" : 0,
  "query" : {
    "term" : {
      "regionId" : {
        "value" : 10,
        "boost" : 1.0
      }
    }
  },
  "_source" : false,
  "stored_fields" : "_none_",
  "aggregations" : {
    "groupby" : {
      "composite" : {
        "size" : 1000,
        "sources" : [
          {
            "firstNameField" : {
              "terms" : {
                "field" : "firstName.keyword",
                "missing_bucket" : true,
                "order" : "asc"
              }
            }
          },
          {
            "lastNameField" : {
              "terms" : {
                "field" : "lastName.keyword",
                "missing_bucket" : true,
                "order" : "asc"
              }
            }
          },
          {
            "emailField" : {
              "terms" : {
                "field" : "emails.email.keyword",
                "missing_bucket" : true,
                "order" : "asc"
              }
            }
          }
        ]
      },
      "aggregations" : {
        "having.3483" : {
          "bucket_selector" : {
            "buckets_path" : {
              "a0" : "_count"
            },
            "script" : {
              "source" : "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a0,params.v0))",
              "lang" : "painless",
              "params" : {
                "v0" : 1
              }
            },
            "gap_policy" : "skip"
          }
        }
      }
    }
  }
}

```

---

<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: [August 17, 2020, 6:03pm UTC](https://discuss.elastic.co/t/using-nested-fields-in-composite-aggregation/241921/2 "2020-08-17T18:03:40Z")

</div>

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