WARNING: Version 5.2 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Value Count Aggregation
editValue Count Aggregation
editA single-value
metrics aggregation that counts the number of values that are extracted from the aggregated documents.
These values can be extracted either from specific fields in the documents, or be generated by a provided script. 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.
{ "aggs" : { "grades_count" : { "value_count" : { "field" : "grade" } } } }
Response:
{ ... "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.
Script
editCounting the values generated by a script:
{ ..., "aggs" : { "grades_count" : { "value_count" : { "script" : { "inline" : "doc['grade'].value", "lang" : "painless" } } } } }
This will interpret the script
parameter as an inline
script with the painless
script language and no script parameters. To use a file script use the following syntax:
{ ..., "aggs" : { "grades_count" : { "value_count" : { "script" : { "file": "my_script", "params" : { "field" : "grade" } } } } } }
for indexed scripts replace the file
parameter with an id
parameter.