# Count of nested documents by aggregation

**URL:** https://discuss.elastic.co/t/count-of-nested-documents-by-aggregation/242362
**Category:** Elasticsearch
**Created:** [July 23, 2020, 3:24pm UTC](https://discuss.elastic.co/t/count-of-nested-documents-by-aggregation/242362 "2020-07-23T15:24:02Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Divyanshu\_Tyagi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divyanshu_tyagi/32/72706_2.png) [@Divyanshu\_Tyagi](https://discuss.elastic.co/u/Divyanshu_Tyagi)
#### Post date: [July 23, 2020, 3:24pm UTC](https://discuss.elastic.co/t/count-of-nested-documents-by-aggregation/242362/1 "2020-07-23T15:24:02Z")

</div>

I have 2 level of nesting in my mapping: `business -> funds -> admins/market` I want count of  
`f_id's` that matches with particular `admin_name`

ES MAPPING

```auto
    {
      "text_index" : {
        "mappings" : {
          "properties" : {
            "buisness_id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              },
            },
            "buisness_type" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "funds" : {
              "type" : "nested",
              "properties" : {
                "f_id" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
                "admins" : {
                  "type" : "nested",
                  "properties" : {
                    "admin_name" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      },
                    },
                    "admin_id" : {
                      "type" : "text",
                        "fields" : {
                          "keyword" : {
                            "type" : "keyword",
                            "ignore_above" : 256
                          }
                        }
                    }
                  }
                },
                "market" : {
                  "type" : "nested",
                  "properties" : {
                    "mkt_name" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      }
                    },
                    "mkt_id" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

```

aggs count query:

```auto
    {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "funds",
                "query": {
                  "nested": {
                    "path": "funds.admins",
                    "query": {
                      "bool": {
                        "must": [
                          {
                            "match": {
                              "funds.admins.admin_name": "NOMAD"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "funds_count": {
          "nested": {
            "path": "funds"
          },
          "aggs": {
            "f_count": {
              "value_count": {
                "field": "funds.f_id.keyword"
              }
            }
          }
        }
      }
    }

```

But the aggs count query is giving the incorrect results

---

<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 20, 2020, 3:24pm UTC](https://discuss.elastic.co/t/count-of-nested-documents-by-aggregation/242362/2 "2020-08-20T15:24:24Z")

</div>

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