# Querying nested objects with in nested objects and aggregation on the same

**URL:** <https://discuss.elastic.co/t/querying-nested-objects-with-in-nested-objects-and-aggregation-on-the-same/133170>\
**Category:** Elasticsearch\
**Created:** [May 24, 2018, 2:25pm UTC](https://discuss.elastic.co/t/querying-nested-objects-with-in-nested-objects-and-aggregation-on-the-same/133170 "2018-05-24T14:25:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramchjob](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramchjob/32/31480_2.png) [@ramchjob](https://discuss.elastic.co/u/ramchjob)\
**Post date:** [May 24, 2018, 2:25pm UTC](https://discuss.elastic.co/t/querying-nested-objects-with-in-nested-objects-and-aggregation-on-the-same/133170/1 "2018-05-24T14:25:13Z")

</div>

Dear ES Gurus,

Have been facing difficulties in querying nested objects with in nested objects.

DOC sample as below:

```
{  
  "Customer":{  
    "Profile":{
      "id": "1234",	
      "name":{  
        "firstName":"Test",
        "lastName":"Test"
      },
      "Memberships":[  
        {  
          "memberId":"1",
          "memberShipType":"Customer",
		  "rank": 1
          "sources":[  
            {  
              "id":"WEB",
              "sourceType":"DERIVED"
            }
          ]
        },
        {  
          "memberId":"1",
          "memberShipType":"Customer",
		  "rank": 2
          "sources":[  
            {  
              "id":"store",
              "sourceType":"ORDER"
            }
          ]
        }
      ]
    }
  }
}

```

Need to query against sources nested object on field "sourceType" : "DERIVED" and pull only those documents do the aggregation on "memberId" under memberships nested object.

The requirement is to find memberid and id with sourceType "DERIVED" and count of memberId unique values.

Hope i am clear, Please let me know

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [June 18, 2018, 9:31pm UTC](https://discuss.elastic.co/t/querying-nested-objects-with-in-nested-objects-and-aggregation-on-the-same/133170/2 "2018-06-18T21:31:18Z")

</div>

If you want to filter documents that can be a part of your aggregation you should use [filter aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html).

For example, below we are filtering docs with `"memberships.memberShipType" : "customer"` to be a part of the agg `by_memberid`:

```auto
{
    "size" : 0,
    "aggs" : {
        "by_nested" : {
            "nested" : {
                "path" : "memberships"
            },
            "aggs" : {
                "by_filter" : {
                     "filter" : { "match": { "memberships.memberShipType" : "customer"}},
                        "aggs" : {
                            "by_memberid" : {
                                "terms" : {"field" : "memberships.memberId"}
                            }
                        }
                }
            }
        }
    }
}

```

---

<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 16, 2018, 9:31pm UTC](https://discuss.elastic.co/t/querying-nested-objects-with-in-nested-objects-and-aggregation-on-the-same/133170/3 "2018-07-16T21:31:21Z")

</div>

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