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

Feat/Add support for weighted average aggregation #197

Merged
merged 4 commits into from
Mar 10, 2024

Conversation

atreids
Copy link
Contributor

@atreids atreids commented Feb 28, 2024

Issues

resolves #154

Aim

Add support for weight average aggregations released in v6.4.0 release notes

A weighted average is ∑(value * weight) / ∑(weight)

Solution

  • Added new WeightedAverageAggregation class which extends MetricsAggregationBase.
  • As the structure of the weight average aggregation is slightly different from other metric aggregations. Add two new methods value and weight to allow the user to set these metrics. The metric can be a string (for a field name), or a script. They both accept an optional second param to set the missing property. Alternatively, the user can set value and weight when constructing the class - but cannot set missing.
  • Overwrote default methods script, field, and missing as these did not make sense as top level methods for this aggregation.
  • Added a test.

Example

const weightedAvgAgg = esb.weightedAverageAggregation(
    'myWeightedAgg',
    'value_field',
    'weighted_field',
);
const body = esb.requestBodySearch().agg(weightedAvgAgg);

Would produce a query with:

{
  "aggs": {
    "myWeightedAgg": {
      "weighted_avg": {
        "value": {
          "field": "value_field"
        },
        "weight": {
          "field": "weighted_field"
        }
      }
    }
  }
}

Alternatively.

const weightedAvgAgg = new WeightedAverageAggregation('my_weighted_agg')
    .value('my_value_field', 5)
    .weight('my_weight_field', 10);
const body = new RequestBodySearch().agg(weightedAvgAgg);

Would produce a query with:

{
  "aggs": {
    "my_weighted_agg": {
      "weighted_avg": {
        "value": {
          "field": "my_value_field",
	  "missing": 5
        },
        "weight": {
          "field": "my_weight_field",
          "missing": 10
        }
      }
    }
  }
}

@sudo-suhas sudo-suhas merged commit 3de7f2d into sudo-suhas:master Mar 10, 2024
9 checks passed
@sudo-suhas
Copy link
Owner

Thanks for the PR @atreids

Copy link

🎉 This PR is included in version 2.27.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

adding support for weighted average aggregation
2 participants