summaryrefslogtreecommitdiff
path: root/docs/reference/aggregations/metrics/valuecount-aggregation.asciidoc
blob: ed5e23ee33906ee9cc794ae59263095f64070cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[[search-aggregations-metrics-valuecount-aggregation]]
=== Value Count Aggregation

A `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.

[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.

==== Script

Counting the values generated by a script:

[source,js]
--------------------------------------------------
{
    ...,

    "aggs" : {
        "grades_count" : { "value_count" : { "script" : "doc['grade'].value" } }
    }
}
--------------------------------------------------

TIP: The `script` parameter expects an inline script. Use `script_id` for indexed scripts and `script_file` for scripts in the `config/scripts/` directory.