Skip to content

Extend the Avg Aggregation to support weighted average #15731

Closed
@lquerel

Description

@lquerel

The current Avg Aggretation only supports the simple form of average (ie: sum(values) / num of values). In order to support weighted average (ie: sum( value * weight) / sum (weight)) in an efficient way, I suggest the following changes in the API.

First implementation without modification of the existing API
The script property in the avg object can return a number of a list of numbers. My proposal is to also support a map containing 2 fixed properties: value and weight (or values and weights if multiple values) (. For example:

{"avg" : { "script" : "[value: doc['latency'].value, weight: doc['transaction'].value]" }}

I prototyped this solution with only 2 modifications. A first modification in the ScriptDoubleValues class to take into account this map. A second modification in the AvgAggregator class to apply the right formula accordingly to the context.

Second implementation requiring minor modifications of the existing API (more efficient because no script)
We can add a new property 'weight' to define the field to use in the document.

{ "avg" : { "field" : "latency",  "weight" : "transaction"} } 

I didn't prototype this solution because I'm still not sure which DoubleValues implementation to use to get efficiently the value and the weight. If you have some advices don't hesitate.

A third option is to use scripted metrics but until now it's not possible to sort the result of scripted metrics so that doesn't work for me. I created a pull request to add this support but I'm still waiting approval. But anyway a solution based on scripted metrics will be less efficient.

This proposal should be easy to implement and I'm pretty sure that many users will be happy with this extended version of the avg aggregation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions