-
Notifications
You must be signed in to change notification settings - Fork 578
adding sum aggregations docs #10256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
adding sum aggregations docs #10256
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,49 +9,199 @@ redirect_from: | |
|
||
# Sum aggregations | ||
|
||
The `sum` metric is a single-value metric aggregations that returns the sum of the values of a field. | ||
The `sum` aggregation is a single-value metric aggregation that calculates the total sum of numeric values extracted from a field across all matching documents. This aggregation is commonly used to compute totals for metrics such as revenue, quantity, or duration. | ||
|
||
The following example calculates the total sum of the `taxful_total_price` field: | ||
## Parameters | ||
|
||
The `sum` aggregation takes the following parameters. | ||
|
||
| Parameter | Data type | Description | | ||
| --------- | --------- | ------------------------------------------------------------------------------------------ | | ||
| `field` | String | The field to aggregate on. Must be a numeric field. | | ||
| `script` | Object | The script to calculate custom values for aggregation. Can be used instead of or with `field`. | | ||
| `missing` | Number | The default value used for documents missing the target field. | ||
|
||
## Example | ||
|
||
The following example demonstrates how to calculate the total weight of deliveries recorded in a logistics index. | ||
|
||
### Create the index | ||
|
||
```json | ||
PUT /deliveries | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"shipment_id": { "type": "keyword" }, | ||
"weight_kg": { "type": "double" } | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
### Add sample documents | ||
|
||
```json | ||
POST /deliveries/_bulk?refresh=true | ||
{"index": {}} | ||
{"shipment_id": "S001", "weight_kg": 12.5} | ||
{"index": {}} | ||
{"shipment_id": "S002", "weight_kg": 7.8} | ||
{"index": {}} | ||
{"shipment_id": "S003", "weight_kg": 15.0} | ||
{"index": {}} | ||
{"shipment_id": "S004", "weight_kg": 10.3} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
### Run the sum aggregation | ||
|
||
The following request computes the total weight across all documents in the `deliveries` index, omits document hits and returns the total sum of `weight_kg`: | ||
|
||
```json | ||
GET /deliveries/_search | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"total_weight": { | ||
"sum": { | ||
"field": "weight_kg" | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
The response contains value of `45.6` corresponding to the sum of `12.5` + `7.8` + `15.0` + `10.3`: | ||
|
||
```json | ||
{ | ||
... | ||
"hits": { | ||
"total": { | ||
"value": 4, | ||
"relation": "eq" | ||
}, | ||
"max_score": null, | ||
"hits": [] | ||
}, | ||
"aggregations": { | ||
"total_weight": { | ||
"value": 45.6 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Using a script to compute values | ||
|
||
Instead of specifying a field directly, you can provide a script to calculate values for the aggregation. This is useful when the value must be derived or adjusted. | ||
|
||
In the following example, each weight is converted from kilograms to grams before summing using a script: | ||
|
||
```json | ||
GET opensearch_dashboards_sample_data_ecommerce/_search | ||
GET /deliveries/_search | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"sum_taxful_total_price": { | ||
"total_weight_grams": { | ||
"sum": { | ||
"field": "taxful_total_price" | ||
"script": { | ||
"source": "doc['weight_kg'].value * 1000" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
#### Example response | ||
The response includes `total_weight_grams` of `45600`: | ||
|
||
```json | ||
{ | ||
"took": 16, | ||
"timed_out": false, | ||
"_shards": { | ||
"total": 1, | ||
"successful": 1, | ||
"skipped": 0, | ||
"failed": 0 | ||
... | ||
"hits": { | ||
"total": { | ||
"value": 4, | ||
"relation": "eq" | ||
}, | ||
"max_score": null, | ||
"hits": [] | ||
}, | ||
"aggregations": { | ||
"total_weight_grams": { | ||
"value": 45600 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Combining a field with a value script | ||
|
||
You can also specify both a `field` and a `script`, using the special variable `_value` to reference the field’s value. This is useful when applying transformations to existing field values. | ||
|
||
The following example increases all weights by 10% before summing: | ||
|
||
```json | ||
GET /deliveries/_search | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"adjusted_weight": { | ||
"sum": { | ||
"field": "weight_kg", | ||
"script": { | ||
"source": "Math.round(_value * 110) / 100.0" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
The response reflects a 10% increase applied to the original total weight. | ||
|
||
```json | ||
{ | ||
... | ||
"hits": { | ||
"total": { | ||
"value": 4675, | ||
"value": 4, | ||
"relation": "eq" | ||
}, | ||
"max_score": null, | ||
"hits": [] | ||
}, | ||
"aggregations": { | ||
"sum_taxful_total_price": { | ||
"value": 350884.12890625 | ||
"adjusted_weight": { | ||
"value": 50.16 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Handling missing values | ||
|
||
Documents missing the target field are ignored by default. To include them using a default value, use the `missing` parameter. This ensures that documents without the field are treated as having zero weight and included in the aggregation. | ||
|
||
The following example assigns a default value of `0` for missing `weight_kg` fields: | ||
|
||
```json | ||
GET /deliveries/_search | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"total_weight_with_missing": { | ||
"sum": { | ||
"field": "weight_kg", | ||
"missing": 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we don't need an example, but a table of parameter supported. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1: @AntonEliatra Could you please add a parameter table for this page (and any aggregations page that doesn't have it)? See for example https://docs.opensearch.org/docs/latest/aggregations/bucket/geohash-grid/#supported-parameters |
||
} | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels pretty similar to the previous example, can we combine them?