mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-05 20:48:22 +00:00
a03b6c2fa5
This commit adds back "id" as the key within a script to specify a stored script (which with file scripts now gone is no longer ambiguous). It also adds "source" as a replacement for "code". This is in an attempt to normalize how scripts are specified across both put stored scripts and script usages, including search template requests. This also deprecates the old inline/stored keys.
83 lines
2.3 KiB
Plaintext
83 lines
2.3 KiB
Plaintext
[[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]
|
|
--------------------------------------------------
|
|
POST /sales/_search?size=0
|
|
{
|
|
"aggs" : {
|
|
"types_count" : { "value_count" : { "field" : "type" } }
|
|
}
|
|
}
|
|
--------------------------------------------------
|
|
// CONSOLE
|
|
// TEST[setup:sales]
|
|
|
|
Response:
|
|
|
|
[source,js]
|
|
--------------------------------------------------
|
|
{
|
|
...
|
|
"aggregations": {
|
|
"types_count": {
|
|
"value": 7
|
|
}
|
|
}
|
|
}
|
|
--------------------------------------------------
|
|
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
|
|
|
The name of the aggregation (`types_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]
|
|
--------------------------------------------------
|
|
POST /sales/_search?size=0
|
|
{
|
|
"aggs" : {
|
|
"type_count" : {
|
|
"value_count" : {
|
|
"script" : {
|
|
"source" : "doc['type'].value"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
--------------------------------------------------
|
|
// CONSOLE
|
|
// TEST[setup:sales]
|
|
|
|
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:
|
|
|
|
[source,js]
|
|
--------------------------------------------------
|
|
POST /sales/_search?size=0
|
|
{
|
|
"aggs" : {
|
|
"types_count" : {
|
|
"value_count" : {
|
|
"script" : {
|
|
"id": "my_script",
|
|
"params" : {
|
|
"field" : "type"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
--------------------------------------------------
|
|
// CONSOLE
|
|
// TEST[setup:sales,stored_example_script]
|