# ElasticSearch Nested Aggregation with Cardinality on another field

**URL:** <https://discuss.elastic.co/t/elasticsearch-nested-aggregation-with-cardinality-on-another-field/119398>\
**Category:** Elasticsearch\
**Created:** [February 11, 2018, 10:47pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregation-with-cardinality-on-another-field/119398 "2018-02-11T22:47:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tu5har](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@Tu5har](https://discuss.elastic.co/u/Tu5har)\
**Post date:** [February 11, 2018, 10:47pm UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregation-with-cardinality-on-another-field/119398/1 "2018-02-11T22:47:13Z")

</div>

Hi All,

I need some help on below problem. Let me know how can I form my query to get expected output. Details posted in the stack overflow page. Thanks

> <https://stackoverflow.com/questions/48737246/elasticsearch-nested-aggregation-with-cardinality-on-another-field>

---

<div class="post-metadata">

**Author:** ![Tu5har](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@Tu5har](https://discuss.elastic.co/u/Tu5har)\
**Post date:** [February 13, 2018, 11:21am UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregation-with-cardinality-on-another-field/119398/2 "2018-02-13T11:21:21Z")

</div>

I got the solution I was looking for. This can be achieved by using reverse nested aggregation.

As per the reference([https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html)),  
When nested aggregation is applied, the query runs against the nested document. So to access any field of parent document inside nested doc, reverse nested aggregation can be used.

The final query looks like:

```
{
  "size": 0,
  "aggregations": {
    "crankings": {
      "nested": {
        "path": "crankings"
      },
      "aggregations": {
        "crankings": {
          "terms": {
            "field": "crankings.name.raw",
            "size": 0
          },
          "aggregations": {
            "internal_cardinality": {
              "reverse_nested": { },
              "aggregations": {
                "cid": {
                  "cardinality": {
                    "field": "cid"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
```

---

<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:** [March 13, 2018, 11:21am UTC](https://discuss.elastic.co/t/elasticsearch-nested-aggregation-with-cardinality-on-another-field/119398/3 "2018-03-13T11:21:31Z")

</div>

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