Elasticsearch: Return number of buckets for terms aggregation

Created on 4 Sep 2014  路  2Comments  路  Source: elastic/elasticsearch

In terms aggregation we can limit the number of buckets using the size parameter but then we are not sure how much buckets exist.
Or we will need to do a separate count aggregation to find this.
Kindly return the total number of distinct terms in the response.

Most helpful comment

@clintongormley: For distinct values, I believe you'd need to use a cardinality aggregation, not value_count:

GET /_search
{
  "aggs": {
    "top_terms": {
      "terms": {
        "field": "foo"
      }
    },
    "distinct_terms": {
      "cardinality": {
        "field": "foo"
      }
    }
  }
}

All 2 comments

Hi @Vineeth-Mohan

Each aggregation is intended to be "minimal". If you want to know how many distinct values there are, you can add a value_count aggregation:

GET /_search
{
  "aggs": {
    "top_terms": {
      "terms": {
        "field": "foo"
      }
    },
    "total_terms": {
      "value_count": {
        "field": "foo"
      }
    }
  }
}

See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html#search-aggregations-metrics-valuecount-aggregation

@clintongormley: For distinct values, I believe you'd need to use a cardinality aggregation, not value_count:

GET /_search
{
  "aggs": {
    "top_terms": {
      "terms": {
        "field": "foo"
      }
    },
    "distinct_terms": {
      "cardinality": {
        "field": "foo"
      }
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings