Value Count Aggregation Documentation & example?

Hello,
I'm looking for more details of Value Count Aggregation because the existing doc dosen't really make it clear.

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

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
	}
  }
1 Like

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