# How to get the total documents count, containing a specific field, using aggregations?

**URL:** <https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713>\
**Category:** Elasticsearch\
**Created:** [January 8, 2016, 10:31am UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713 "2016-01-08T10:31:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhay](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Abhay](https://discuss.elastic.co/u/Abhay)\
**Post date:** [January 8, 2016, 10:31am UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713/1 "2016-01-08T10:31:08Z")

</div>

I am moving from ElasticSearch 1.7 to 2.0. Previously while calculating Term Facets I got the Total Count as well. This will tell in how many documents that field exists. This is how I was doing previously.

```
TermsFacet termsFacet = (TermsFacet) facet;
termsFacet.getTotalCount();

```

It worked with Multivalue field as well.

Now in current version for Term Aggregation we don't have anything as Total Count. I am getting DocCount inside Aggregation bucket. But that will not work for muti-valued fields.

```
Terms termsAggr = (Terms) aggr;
for (Terms.Bucket bucket : termsAggr.getBuckets()) {
                    String bucketKey = bucket.getKey();
                    totalCount += bucket.getDocCount();
 }

```

Is there any way I can get Total count of the field from term aggregation. As I am already firing Term Aggregation query, I don't want to fire **exists Filter** query. Is there any way to do this using aggregations.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 8, 2016, 11:01am UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713/2 "2016-01-08T11:01:45Z")

</div>

You can use value count I think. See [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/\_metrics\_aggregations.html#java-aggs-metrics-valuecount](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_metrics_aggregations.html#java-aggs-metrics-valuecount)

---

<div class="post-metadata">

**Author:** ![Abhay](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Abhay](https://discuss.elastic.co/u/Abhay)\
**Post date:** [January 10, 2016, 2:18am UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713/3 "2016-01-10T02:18:21Z")

</div>

I think this "Value Count" aggregate counts the value of the Field mentioned in the query. This will not work for multi-value fields. e.g If I have 2 documents with Field "Name" and that field has value as ["John", "Jack", "Reacher"] in one document and ["Jack","Reacher"] in 2nd document then this will give me result as 5, because it will count the values. But I want only the field count which is 2.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 10, 2016, 5:07am UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713/4 "2016-01-10T05:07:31Z")

</div>

getDocCount Should return 2, right?

---

<div class="post-metadata">

**Author:** ![Abhay](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Abhay](https://discuss.elastic.co/u/Abhay)\
**Post date:** [January 11, 2016, 4:11am UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713/5 "2016-01-11T04:11:06Z")

</div>

I am not able to find `getDocCount()` method in ValueCount aggregation response.

```
    ValueCount agg = response.getAggregations().get("ValueCountAgg");
            long value = agg.getValue();
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 11, 2016, 5:46am UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713/6 "2016-01-11T05:46:56Z")

</div>

I meant that TermsAgg should already give you 2 so you don't need a sub aggregation.

---

<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:25pm UTC](https://discuss.elastic.co/t/how-to-get-the-total-documents-count-containing-a-specific-field-using-aggregations/38713/7 "2017-07-05T23:25:32Z")

</div>


