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.
[source,js]
--------------------------------------------------
{
"aggs" : {
"grades_count" : { "value_count" : { "field" : "grade" } }
}
}
--------------------------------------------------
Response:
[source,js]
--------------------------------------------------
{
...
"aggregations": {
"grades_count": {
"value": 10
}
}
}
--------------------------------------------------
The name of the aggregation (`grades_count` above) also serves as the key by which the aggregation result can be
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:
[source,js]
--------------------------------------------------
{
...,
"aggs" : {
"grades_count" : { "value_count" : { "script" : "doc['grade'].value" } }
}
}
2014-02-28 09:28:50 -05:00
--------------------------------------------------
2015-04-26 11:30:38 -04:00
2015-05-12 05:37:22 -04:00
This will interpret the `script` parameter as an `inline` script with the default script language and no script parameters. To use a file script use the following syntax:
[source,js]
--------------------------------------------------
{
...,
"aggs" : {
"grades_count" : {
"value_count" : {
"script" : {
"file": "my_script",
"params" : {
"field" : "grade"
}
}
}
}
}
}
--------------------------------------------------
TIP: for indexed scripts replace the `file` parameter with an `id` parameter.