# Value Count Aggregation Documentation & example?

**URL:** <https://discuss.elastic.co/t/value-count-aggregation-documentation-example/165029>\
**Category:** Elasticsearch\
**Created:** [January 21, 2019, 10:27am UTC](https://discuss.elastic.co/t/value-count-aggregation-documentation-example/165029 "2019-01-21T10:27:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wassim\_Ben\_Amor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wassim_ben_amor/32/39799_2.png) [@Wassim\_Ben\_Amor](https://discuss.elastic.co/u/Wassim_Ben_Amor)\
**Post date:** [January 21, 2019, 10:27am UTC](https://discuss.elastic.co/t/value-count-aggregation-documentation-example/165029/1 "2019-01-21T10:27:25Z")

</div>

Hello,  
I'm looking for more details of Value Count Aggregation because the [existing doc](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-metrics-valuecount-aggregation.html) dosen't really make it clear.

I'm looking for a real example please.  
Thank you

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [January 21, 2019, 12:39pm UTC](https://discuss.elastic.co/t/value-count-aggregation-documentation-example/165029/2 "2019-01-21T12:39:56Z")

</div>

It's not the number of unique values (for that see the `cardinality` aggregation). Instead it's the number of uses of _any_ value. This includes repeated uses in an array. The docs had an example of why that might be useful:

> Typically, this aggregator will be used in conjunction with other single-value aggregations. For example, when computing the `avg` one might be interested in the number of values the average is computed over.

So for the given example:

```
DELETE test
POST test/_doc/1
{
  "my_field":1
}
POST test/_doc/2
{
  "my_field":[1,1,2]
}
GET test/_search
{
  "aggs":{
	"cardinality":{
	  "cardinality": {
		"field": "my_field"
	  }
	},
	"value_count":{
	  "value_count": {
		"field": "my_field"
	  }
	},
	"sum":{
	  "sum": {
		"field": "my_field"
	  }
	},
	"avg":{
	  "avg": {
		"field": "my_field"
	  }
	}

  }
}

```

The results are:

```
  "aggregations" : {
	"avg" : {
	  "value" : 1.25
	},
	"sum" : {
	  "value" : 5.0
	},
	"value_count" : {
	  "value" : 4
	},
	"cardinality" : {
	  "value" : 2
	}
  }

```

---

<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:** [February 18, 2019, 12:39pm UTC](https://discuss.elastic.co/t/value-count-aggregation-documentation-example/165029/3 "2019-02-18T12:39:56Z")

</div>

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