sudo-suhas / elastic-builder

A Node.js implementation of the elasticsearch Query DSL :construction_worker:
https://elastic-builder.js.org
MIT License
508 stars 75 forks source link

Feat/Add support for weighted average aggregation #197

Closed atreids closed 7 months ago

atreids commented 7 months ago

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

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 commented 7 months ago

Thanks for the PR @atreids

github-actions[bot] commented 7 months ago

:tada: This PR is included in version 2.27.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: