# How to filter aggregated data on the nested object

**URL:** <https://discuss.elastic.co/t/how-to-filter-aggregated-data-on-the-nested-object/306264>\
**Category:** Elasticsearch\
**Created:** [June 2, 2022, 7:26pm UTC](https://discuss.elastic.co/t/how-to-filter-aggregated-data-on-the-nested-object/306264 "2022-06-02T19:26:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shri\_palchuri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shri_palchuri/32/51976_2.png) [@shri\_palchuri](https://discuss.elastic.co/u/shri_palchuri)\
**Post date:** [June 2, 2022, 7:26pm UTC](https://discuss.elastic.co/t/how-to-filter-aggregated-data-on-the-nested-object/306264/1 "2022-06-02T19:26:39Z")

</div>

Here is the payload used to filter the data, holdings is a nested type and in the aggregation it includes non matched count.

```auto

{"size":0,"query":{"bool":{"filter":[{"bool":{"filter":[{"bool":{"should":[{"bool":{"filter":[{"terms":{"keyInformation.enterpriseHandle":["XXXXXXXXXXXX"],"boost":1}}],"adjust_pure_negative":true,"boost":1}}],"adjust_pure_negative":true,"boost":1}},{"bool":{"filter":[{"bool":{"filter":[{"nested":{"query":{"bool":{"filter":[{"wildcard":{"holdings.parentStyle":{"wildcard":" *Commodities* ","case_insensitive":true,"boost":1}}}],"adjust_pure_negative":true,"boost":1}},"path":"holdings","ignore_unmapped":false,"score_mode":"max","boost":1}}],"adjust_pure_negative":true,"boost":1}}],"adjust_pure_negative":true,"boost":1}}],"adjust_pure_negative":true,"boost":1}}],"adjust_pure_negative":true,"boost":1}},"_source":{"includes":["keyInformation.portfolioHandle"],"excludes":[]},"aggregations":{"parentStyle":{"nested":{"path":"holdings"},"aggregations":{"parentStyle":{"composite":{"size":600,"sources":[{"parentStyle":{"terms":{"field":"holdings.parentStyle","missing_bucket":false,"order":"asc"}}}]},"aggregations":{"count":{"reverse_nested":{},"aggregations":{"count":{"value_count":{"field":"keyInformation.portfolioHandle"}}}}}}}}}}

```

Output:

```auto
"aggregations" : {
    "parentStyle" : {
      "doc_count" : 204,
      "parentStyle" : {
        "after_key" : {
          "parentStyle" : "U.S. Stocks"
        },
        "buckets" : [
          
          {
            "key" : {
              "parentStyle" : "Cash"
            },
            "doc_count" : 16,
            "count" : {
              "doc_count" : 14,
              "count" : {
                "value" : 14
              }
            }
          },
          {
            "key" : {
              "parentStyle" : "Commodities"
            },
            "doc_count" : 23,
            "count" : {
              "doc_count" : 14,
              "count" : {
                "value" : 14
              }
            }
          },
          {
            "key" : {
              "parentStyle" : "U.S. Stocks"
            },
            "doc_count" : 81,
            "count" : {
              "doc_count" : 14,
              "count" : {
                "value" : 14
              }
            }
          }
        ]
      }
    }
  }
}

```

But I am looking to return count only for "parentStyle" : "Commodities", how can I achieve this?

---

<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, 2022, 7:26pm UTC](https://discuss.elastic.co/t/how-to-filter-aggregated-data-on-the-nested-object/306264/2 "2022-06-30T19:26:47Z")

</div>

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