Skip to content
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

Adding weighted average documentation page #5

Open
wants to merge 4 commits into
base: integ/weighted_average
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _aggregations/metric/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ OpenSearch supports the following metric aggregations:
- [Stats]({{site.url}}{{site.baseurl}}/aggregations/metric/stats/)
- [Sum]({{site.url}}{{site.baseurl}}/aggregations/metric/sum/)
- [Top hits]({{site.url}}{{site.baseurl}}/aggregations/metric/top-hits/)
- [Value count]({{site.url}}{{site.baseurl}}/aggregations/metric/value-count/)
- [Value count]({{site.url}}{{site.baseurl}}/aggregations/metric/value-count/)
- [Weighted average]({{site.url}}{{site.baseurl}}/aggregations/metric/weighted-average/)
96 changes: 96 additions & 0 deletions _aggregations/metric/weighted-average.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
layout: default
title: Weighted Average
parent: Metric aggregations
grand_parent: Aggregations
nav_order: 150
redirect_from:
- /query-dsl/aggregations/metric/weighted-average/
---

# Weighted average aggregations

The `weighted average` metric is a multi-value metric aggregations that returns the weighted average value. Weighted average can be used when calculating grades for students, where various assignments and exams make-up the final grade of 100%.

A weighted average has two fields: a value and a weight.

* `value`: value that the weight is applied to
* `missing`: (optional) to apply a default value for any missing/null value fields
* `weight`: weight that the value is applied for
* `missing`: (optional) to apply a default weight for any missing/null weight fields

Every value field is applied with the respective value in the `weight` field.

weighted_average = ∑xw/∑w

```json
GET opensearch_dashboards_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"weighted_average_response": {
"weighted_avg": {
"value": {
"field": "taxless_total_price"
},
"weight": {
"field": "total_quantity"
}
}
}
}
}
```
{% include copy-curl.html %}

#### Example response

```json
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4675,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"weighted_average_response": {
"value": 81.8645153323506
}
}
}
```

#### Example with optional `missing` field

```json
GET opensearch_dashboards_sample_data_ecommerce/_search
{
"size": 0,
"aggs": {
"weighted_average_response": {
"weighted_avg": {
"value": {
"field": "taxless_total_price",
"missing": 100
},
"weight": {
"field": "total_quantity",
"missing": 1
}
}
}
}
}
```
{% include copy-curl.html %}
Loading