# How to get a count from an aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-get-a-count-from-an-aggregation/112681>\
**Category:** Elasticsearch\
**Created:** [December 20, 2017, 4:43pm UTC](https://discuss.elastic.co/t/how-to-get-a-count-from-an-aggregation/112681 "2017-12-20T16:43:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kris\_Hermstad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kris_hermstad/32/25867_2.png) [@Kris\_Hermstad](https://discuss.elastic.co/u/Kris_Hermstad)\
**Post date:** [December 20, 2017, 4:43pm UTC](https://discuss.elastic.co/t/how-to-get-a-count-from-an-aggregation/112681/1 "2017-12-20T16:43:33Z")

</div>

Sorry if this sounds like a duplicate. I've been reading all the various count/aggregation threads and I haven't really been able to figure this one out yet.

```
POST /profile/_search
{
  "size": 0,
  "aggregations": {
    "az_profile": {
      "nested": {
        "path": "az"
      },
      "aggregations": {
        "duplicateCount": {
          "terms": {
            "field": "az.account",
            "min_doc_count": 2,
            "size": 1000
          }
        }
      }
    }
  }
}

```

This returns me the counts each az.account with a minimum count of 2.

Now how would I add an aggregation to get a count for how many az.accounts that aggregation returned?

I basically need an integer count on my first aggregated query

---

<div class="post-metadata">

**Author:** ![Kris\_Hermstad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kris_hermstad/32/25867_2.png) [@Kris\_Hermstad](https://discuss.elastic.co/u/Kris_Hermstad)\
**Post date:** [December 20, 2017, 4:58pm UTC](https://discuss.elastic.co/t/how-to-get-a-count-from-an-aggregation/112681/2 "2017-12-20T16:58:54Z")

</div>

hey, so I figured it out!

```
POST /profile/_search
{
  "size": 0,
  "aggregations": {
    "az_profile": {
      "nested": {
        "path": "az"
      },
      "aggregations": {
        "duplicateCount": {
          "terms": {
            "field": "az.visitor",
            "min_doc_count": 2,
            "size": 1000
          }
        },
        "distinct_count":{
          "cardinality":{
            "field": "az.visitor"
          }
        }
      }
    }
  }
}

```

Is there anyway to only get back the cardinality part of my aggregation?

---

<div class="post-metadata">

**Author:** ![Kris\_Hermstad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kris_hermstad/32/25867_2.png) [@Kris\_Hermstad](https://discuss.elastic.co/u/Kris_Hermstad)\
**Post date:** [December 20, 2017, 7:47pm UTC](https://discuss.elastic.co/t/how-to-get-a-count-from-an-aggregation/112681/3 "2017-12-20T19:47:43Z")

</div>

that was actually pretty easy, I just had to remove the middle aggregation section of my previous aggregate.

Thanks for Being My Rubber Duck, discuss.elastic 😛

---

<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:** [January 17, 2018, 7:47pm UTC](https://discuss.elastic.co/t/how-to-get-a-count-from-an-aggregation/112681/4 "2018-01-17T19:47:44Z")

</div>

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