# Aggregation on nested document with reverse\_nested to count unique parent

**URL:** <https://discuss.elastic.co/t/aggregation-on-nested-document-with-reverse-nested-to-count-unique-parent/29624>\
**Category:** Elasticsearch\
**Created:** [September 19, 2015, 7:24am UTC](https://discuss.elastic.co/t/aggregation-on-nested-document-with-reverse-nested-to-count-unique-parent/29624 "2015-09-19T07:24:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrisp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrisp/32/4705_2.png) [@andrisp](https://discuss.elastic.co/u/andrisp)\
**Post date:** [September 19, 2015, 7:24am UTC](https://discuss.elastic.co/t/aggregation-on-nested-document-with-reverse-nested-to-count-unique-parent/29624/1 "2015-09-19T07:24:32Z")

</div>

I have document type "ads" (title, url and similar fields) which have nested documents "views" (view time, user, date of birth, country and similar fields).

I'm trying to count ads per country, first by filtering on parent ("title") and nested docs ("date of birth").

I'm using reverse\_nested to get this doc count. The problem is that "country\_count" aggregation on the nested documents "views.country" returns me TOTAL number of the parent documents who has a nested "views" with this country. It ignores date of birth in the nested documents.

For example, if "ads" document has two "views":

1. country: UK, dob: 1985-01-01
2. country: DE, dob: 1970-01-01

Then following aggregation will return that both UK an DE have one document matching:

```
"countries": {
    "filter": {                        
        "and": [
            {"term": {"title": "free beer"}},
            {
                "nested": {
                    "path": "views",
                    "filter": {
                        "and": [
                            {"range": {"views.dob": {"gte": "1980-09-15", "lt": "1990-09-15"}}}
                        ]
                    }
                }
            }
        ]
    },       
    "aggs": {
        "in_to_views": {
            "nested": {
                "path" : "views"
            },
            "aggs": {
                "country_count": {
                    "terms": {"field": "views.country"},
                    "aggs": {
                        "back_to_ads": {"reverse_nested": {}}
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 23, 2015, 10:05am UTC](https://discuss.elastic.co/t/aggregation-on-nested-document-with-reverse-nested-to-count-unique-parent/29624/2 "2015-09-23T10:05:30Z")

</div>

The `nested` aggregator delegates to all nested docs that parent doc matches with.  
The easiest to get what you want is to repeat the `range` filter in the aggregations like this:

```auto
{
  "aggs": {
    "in_to_views": {
      "nested": {
        "path": "views"
      },
      "aggs": {
        "my-filter": {
          "filter": {
            "range": {
              "views.dob": {
                "gte": "1980-09-15",
                "lt": "1990-09-15"
              }
            }
          },
          "aggs": {
            "country_count": {
              "terms": {
                "field": "views.country"
              },
              "aggs": {
                "back_to_ads": {
                  "reverse_nested": {}
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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 5, 2017, 11:48pm UTC](https://discuss.elastic.co/t/aggregation-on-nested-document-with-reverse-nested-to-count-unique-parent/29624/3 "2017-07-05T23:48:31Z")

</div>


