# Get relevant counts for inner nested objects based on query

**URL:** <https://discuss.elastic.co/t/get-relevant-counts-for-inner-nested-objects-based-on-query/235414>\
**Category:** Elasticsearch\
**Created:** [June 2, 2020, 9:42pm UTC](https://discuss.elastic.co/t/get-relevant-counts-for-inner-nested-objects-based-on-query/235414 "2020-06-02T21:42:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hassan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hassan/32/69589_2.png) [@hassan](https://discuss.elastic.co/u/hassan)\
**Post date:** [June 2, 2020, 9:42pm UTC](https://discuss.elastic.co/t/get-relevant-counts-for-inner-nested-objects-based-on-query/235414/1 "2020-06-02T21:42:35Z")

</div>

I'm trying to get the counts of my leads object and contacts objects based on the query

e.g below is my index

```
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "Affiliate",
              "participant" : "Seychelles",
              "name" : "Leroy Smith",
              "contacts" : [
                {
                  "promotion" : "3 days 4 nights Free Camping",
                  "contactType" : "Mail"
                },
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Email"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "Affiliate",
              "participant" : "Marketing Info",
              "name" : "Edwin Stone",
              "contacts" : [
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Email"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "VIC",
              "participant" : "CAMPING WORLD - SURFSIDE",
              "name" : "Dustin Hall",
              "contacts" : [
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Mail"
                },
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Five9"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "Web",
              "participant" : "ROCKYFORKRANCHRESORT.COM",
              "name" : "Martin Reyes",
              "contacts" : [
                {
                  "promotion" : "3 days 4 nights Free Camping",
                  "contactType" : "SMS"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "Mail",
              "participant" : "Vinguage",
              "name" : "Jesse Johnson",
              "contacts" : [
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Email"
                },
                {
                  "promotion" : "3 days 4 nights Free Camping",
                  "contactType" : "SMS"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "VIC",
              "participant" : "JACKSONVILLE RV MEGA SHOW",
              "name" : "Patricia Schroeder MD",
              "contacts" : [
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "SMS"
                },
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "SMS"
                },
                {
                  "promotion" : "3 days 4 nights Free Camping",
                  "contactType" : "Five9"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "VIC",
              "participant" : "CAMPING WORLD - SURFSIDE",
              "name" : "Steven Smith",
              "contacts" : [
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Five9"
                },
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "SMS"
                },
                {
                  "promotion" : "3 days 4 nights Free Camping",
                  "contactType" : "SMS"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "Mail",
              "participant" : "Vinguage",
              "name" : "Aaron Ferguson",
              "contacts" : [
                {
                  "promotion" : "3 days 4 nights Free Camping",
                  "contactType" : "SMS"
                },
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Five9"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "Web",
              "participant" : "WALLYWORLDRESORT.COM",
              "name" : "Faith Flores",
              "contacts" : [
                {
                  "promotion" : "3 days 4 nights Free Camping",
                  "contactType" : "Email"
                }
              ]
            }
          ]
        }
      },
      {
        "_index" : "myleads",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.0,
        "_source" : {
          "leads" : [
            {
              "program" : "VIC",
              "participant" : "CAMPING WORLD - ROANOKE",
              "name" : "Dr. Heather Collins",
              "contacts" : [
                {
                  "promotion" : "21 Days Free Camping",
                  "contactType" : "Mail"
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

```

I want to query or perform aggregations in a way that my result does not contain some inner objects (contacts in my case) which does not match the query. e.g I want to get the counts of contacts where contactType = Mail.

Right now when I apply query on contactType = Mail. I get inner objects with contactType Mail as well as other contacts with diff contactType because they belong to same parent document.

Please help, I want to get the correct count of my leads and contacts. Similarly If I try to generate buckets based on this contactType=Mail query, I get other stuff too in my contactType bucket.

---

<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:** [June 30, 2020, 9:57pm UTC](https://discuss.elastic.co/t/get-relevant-counts-for-inner-nested-objects-based-on-query/235414/2 "2020-06-30T21:57:29Z")

</div>

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