# Nested Aggregation not returning document count

**URL:** <https://discuss.elastic.co/t/nested-aggregation-not-returning-document-count/341454>\
**Category:** Elasticsearch\
**Created:** [August 23, 2023, 9:49am UTC](https://discuss.elastic.co/t/nested-aggregation-not-returning-document-count/341454 "2023-08-23T09:49:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raju\_Yadav](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raju_yadav/32/124827_2.png) [@Raju\_Yadav](https://discuss.elastic.co/u/Raju_Yadav)\
**Post date:** [August 23, 2023, 9:49am UTC](https://discuss.elastic.co/t/nested-aggregation-not-returning-document-count/341454/1 "2023-08-23T09:49:56Z")

</div>

i have a product document and that product is published into various eCommerce website like amazon , flipkart as shown in location field. The product published is a nested field in Elasticsearch. now i want to aggregate that like in a month how many places i published and i need data for last 6 months including current month. like in month of August i published in Amazon and Flipkart then it should show count as 2 for month August.

```auto
"product_published" : [
            {
              "id" : "71",
              "published_on" : 1687769266,
              "location" : "amazon",
              
            },
            {
              "publisher_id" : "71",
              "published_on" : 1687770750,
              "location" : "flipkart",
             },
            {
              "publisher_id" : 71,
              "published_on" : 1687806928,
              "location" : "ebay"
            },
            {
              "publisher_id" : 71,
              "published_on" : 1687806928,
              "location" : "amazon-india"
            },
            {
              "publisher_id" : 71,
              "published_on" : 1687806928,
              "location" : "walmart"
            },
            {
              "publisher_id" : "71",
              "published_on" : 1687771848,
              "location" : "daraz",
              },
            {
              "publisher_id" : "71",
             "published_on" : 1687771955,
              "location" : "bigbasket",
            },
            {
              "publisher_id" : "71",
              "published_on" : 1687772089,
              "location" : "blinkit",
             }
          ]

```

My Aggregation query :

```auto
"aggs": {
    "count_product_published": {
      "nested": {
        "path": "product_published"
      },
      "aggs": {
        "product_published": {
          "date_histogram": {
            "script": "if (doc.containsKey('product_published')) { return Instant.ofEpochMilli(doc['product_published.published_on'].value).atZone(ZoneId.of('UTC')).toLocalDate();} ",
            "calendar_interval": "month",
            "format": "yyyy-MM",
            "time_zone": "UTC",
            "extended_bounds": {
              "min": "now-6M/M",
              "max": "now/M"
            }
          },
          "aggs": {
            "published_count": {
              "terms": {
                "field": "product_published.location.keyword"
              }
            }
          }
        }
      }
    }
  }

```

Result returned :

```auto
"aggregations" : {
    "count_product_published" : {
      "doc_count" : 1796211,
      "product_published" : {
        "buckets" : [
          {
            "key_as_string" : "2023-02",
            "key" : 1675209600000,
            "doc_count" : 0,
            "published_count" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : []
            }
          },
          {
            "key_as_string" : "2023-03",
            "key" : 1677628800000,
            "doc_count" : 0,
            "published_count" : {
              "doc_count_error_upper_bound" : 0,
              "sum_other_doc_count" : 0,
              "buckets" : []
            }
          }

```

It is returning document count as 0.

---

<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:** [September 20, 2023, 9:50am UTC](https://discuss.elastic.co/t/nested-aggregation-not-returning-document-count/341454/2 "2023-09-20T09:50:10Z")

</div>

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