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


(Abhay Verma) #1

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.


(David Pilato) #2

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


(Abhay Verma) #3

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.


(David Pilato) #4

getDocCount Should return 2, right?


(Abhay Verma) #5

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

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

(David Pilato) #6

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


(system) #7