2013-11-29 06:35:25 -05:00
[[search-aggregations-metrics-valuecount-aggregation]]
2014-05-12 19:35:58 -04:00
=== Value Count Aggregation
2013-11-29 06:35:25 -05:00
A `single-value` metrics aggregation that counts the number of values that are extracted from the aggregated documents.
2014-02-04 05:52:45 -05:00
These values can be extracted either from specific fields in the documents, or be generated by a provided script. Typically,
2013-11-29 06:35:25 -05:00
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.
2020-05-04 06:23:02 -04:00
`value_count` does not de-duplicate values, so even if a field has duplicates (or a script generates multiple
identical values for a single document), each value will be counted individually.
2019-09-05 10:11:25 -04:00
[source,console]
2013-11-29 06:35:25 -05:00
--------------------------------------------------
2017-01-23 10:04:53 -05:00
POST /sales/_search?size=0
2013-11-29 06:35:25 -05:00
{
"aggs" : {
2017-01-23 10:04:53 -05:00
"types_count" : { "value_count" : { "field" : "type" } }
2013-11-29 06:35:25 -05:00
}
}
--------------------------------------------------
2017-01-23 10:04:53 -05:00
// TEST[setup:sales]
2013-11-29 06:35:25 -05:00
Response:
2019-09-06 16:09:09 -04:00
[source,console-result]
2013-11-29 06:35:25 -05:00
--------------------------------------------------
{
...
"aggregations": {
2017-01-23 10:04:53 -05:00
"types_count": {
"value": 7
2013-11-29 06:35:25 -05:00
}
}
}
--------------------------------------------------
2017-01-23 10:04:53 -05:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2013-11-29 06:35:25 -05:00
2017-05-15 14:08:46 -04:00
The name of the aggregation (`types_count` above) also serves as the key by which the aggregation result can be
2013-11-29 06:35:25 -05:00
retrieved from the returned response.
2014-02-04 05:52:45 -05:00
==== Script
2015-04-26 11:30:38 -04:00
2014-02-04 05:52:45 -05:00
Counting the values generated by a script:
2019-09-05 10:11:25 -04:00
[source,console]
2014-02-04 05:52:45 -05:00
--------------------------------------------------
2017-01-23 10:04:53 -05:00
POST /sales/_search?size=0
2014-02-04 05:52:45 -05:00
{
"aggs" : {
2017-01-23 10:04:53 -05:00
"type_count" : {
"value_count" : {
2016-06-27 09:55:16 -04:00
"script" : {
2017-06-09 11:29:25 -04:00
"source" : "doc['type'].value"
2016-06-27 09:55:16 -04:00
}
}
}
2014-02-04 05:52:45 -05:00
}
}
2014-02-28 09:28:50 -05:00
--------------------------------------------------
2017-01-23 10:04:53 -05:00
// TEST[setup:sales]
2015-04-26 11:30:38 -04:00
2017-05-17 17:42:25 -04:00
This will interpret the `script` parameter as an `inline` script with the `painless` script language and no script parameters. To use a stored script use the following syntax:
2015-05-12 05:37:22 -04:00
2019-09-05 10:11:25 -04:00
[source,console]
2015-05-12 05:37:22 -04:00
--------------------------------------------------
2017-01-23 10:04:53 -05:00
POST /sales/_search?size=0
2015-05-12 05:37:22 -04:00
{
"aggs" : {
2017-05-15 14:08:46 -04:00
"types_count" : {
2017-01-23 10:04:53 -05:00
"value_count" : {
2015-05-12 05:37:22 -04:00
"script" : {
2017-06-09 11:29:25 -04:00
"id": "my_script",
2015-05-12 05:37:22 -04:00
"params" : {
2017-01-23 10:04:53 -05:00
"field" : "type"
2015-05-12 05:37:22 -04:00
}
}
}
}
}
}
--------------------------------------------------
2017-05-17 17:42:25 -04:00
// TEST[setup:sales,stored_example_script]
2020-05-04 06:23:02 -04:00
NOTE:: Because `value_count` is designed to work with any field it internally treats all values as simple bytes.
Due to this implementation, if `_value` script variable is used to fetch a value instead of accessing the field
directly (e.g. a "value script"), the field value will be returned as a string instead of it's native format.
[[search-aggregations-metrics-valuecount-aggregation-histogram-fields]]
==== Histogram fields
When the `value_count` aggregation is computed on <<histogram,histogram fields>>, the result of the aggregation is the sum of all numbers
in the `counts` array of the histogram.
For example, for the following index that stores pre-aggregated histograms with latency metrics for different networks:
[source,console]
--------------------------------------------------
PUT metrics_index/_doc/1
{
"network.name" : "net-1",
"latency_histo" : {
"values" : [0.1, 0.2, 0.3, 0.4, 0.5],
"counts" : [3, 7, 23, 12, 6] <1>
}
}
PUT metrics_index/_doc/2
{
"network.name" : "net-2",
"latency_histo" : {
"values" : [0.1, 0.2, 0.3, 0.4, 0.5],
"counts" : [8, 17, 8, 7, 6] <1>
}
}
POST /metrics_index/_search?size=0
{
"aggs" : {
"total_requests" : {
"value_count" : { "field" : "latency_histo" }
}
}
}
--------------------------------------------------
For each histogram field the `value_count` aggregation will sum all numbers in the `counts` array <1>.
Eventually, it will add all values for all histograms and return the following result:
[source,console-result]
--------------------------------------------------
{
...
"aggregations" : {
"total_requests" : {
"value" : 97
}
}
}
--------------------------------------------------
// TESTRESPONSE[skip:test not setup]