Skip to content

Aggregate metric double use average#142135

Merged
gmarouli merged 31 commits intoelastic:mainfrom
gmarouli:aggregate-metric-double-use-average
Feb 25, 2026
Merged

Aggregate metric double use average#142135
gmarouli merged 31 commits intoelastic:mainfrom
gmarouli:aggregate-metric-double-use-average

Conversation

@gmarouli
Copy link
Copy Markdown
Contributor

@gmarouli gmarouli commented Feb 9, 2026

In this PR we replace the "derived metric" introduced in #141877 with a script average or if the aggregate_metric_double has only one metric, with that metric.

We support the following scenarios:

  • Single metric: we introduce the concept of a single metric, this is set only if the aggregate_metric_double has only one value. We saw this being used in tests when we aggregate using the min or max, but we did provide full support just in case it's used.
  • Average is available: when aggregate_metric_double has the sum and value_count then we calculate the average. This is used in scripts, sorting and filtering queries.
  • Finally, we return no results in filtering, if none of the above apply.

Part of #128356

@gmarouli gmarouli changed the title [PoC] Aggregate metric double use average [WIP] Aggregate metric double use average Feb 12, 2026
@gmarouli gmarouli added >enhancement :StorageEngine/Mapping The storage related side of mappings labels Feb 13, 2026
@gmarouli gmarouli marked this pull request as ready for review February 13, 2026 08:34
@gmarouli gmarouli changed the title [WIP] Aggregate metric double use average Aggregate metric double use average Feb 13, 2026
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @gmarouli, I've created a changelog YAML for you.

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-storage-engine (Team:StorageEngine)

@gmarouli
Copy link
Copy Markdown
Contributor Author

Kind reminder to @felixbarny & @romseygeek can you take a look at this PR please?

Copy link
Copy Markdown
Member

@carsonip carsonip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did not look at the implementation (can someone else review the code please?), but approving based on the previous approval that it should not break apm based on apm's usage.


private final EnumMap<Metric, NumberFieldMapper.NumberFieldType> metricFields;
// If there is only one metric configured for this field, we delegate to the field.
private final Metric singleMetric;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this would be simpler to handle at the top level, ie if there's only a single metric configured then we just return that subfield as the top-level MappedFieldType? Or do we definitely always need a full AggregateMetricDoubleFieldType for the Mapper to work properly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be cool, it would reduce the conditionals that are scattered around. However, I am concerned about how we would preserve the semantics of the individual metric.

For example, when downsampling, if we see a NumberFieldType we will treat it differently than if we see an AggregateMetricDoubleFieldType even if it has a single metric. I think the same might be happening with aggregations. We still have that name of the field that could help my-aggregate.min but this is ambiguous since it could be either a sub-object or an aggregate metric double.

Am I missing anything?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I missing anything?

More likely that I am, as I'm not that familiar with how downsampling interacts here! Let's keep things as they are for now and maybe investigate simplifying it in a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave here some notes for reference:

Downsampling
The field value fetchers identify that a field, for example, my-metric is an aggregate_metric_double via its type:

            if (fieldType instanceof AggregateMetricDoubleFieldMapper.AggregateMetricDoubleFieldType aggMetricFieldType) {
                fetchers.addAll(AggregateMetricDoubleFieldValueFetcher.create(context, aggMetricFieldType, samplingMethod));
            } else {

Then we create a dedicated downsampler for each metric and downsamples it accordingly. For example, for my-metric.min it will keep the min of the values, for my-metric.sum it will sum all the values and so on. This is how it works even if my-metric has only one metric.

Aggregations
Furthermore, an aggregate metric double only supports the min, max, avg, sum and count aggregations by using the equivalent metric. An aggregate metric double with only min and max, won't be able to calculate the sum for example. I think this behaviour will also change if we switch to using a NumberFieldType.

Conclusion

I am not saying we shouldn't do it, because maybe the above examples are not relevant in real word cases of a single metric. But as you said it's something that we should investigate on its own.

Copy link
Copy Markdown
Contributor

@romseygeek romseygeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@gmarouli gmarouli added the auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Feb 25, 2026
@gmarouli gmarouli merged commit 3db94ef into elastic:main Feb 25, 2026
34 of 35 checks passed
@gmarouli gmarouli deleted the aggregate-metric-double-use-average branch February 25, 2026 13:29
smalyshev pushed a commit to smalyshev/elasticsearch that referenced this pull request Feb 25, 2026
In this PR we replace the "derived metric" introduced in elastic#141877 with a script average or if the `aggregate_metric_double` has only one metric, with that metric.

We support the following scenarios:

- Single metric: we introduce the concept of a single metric, this is set only if the aggregate_metric_double has only one value. We saw this being used in tests when we aggregate using the min or max, but we did provide full support just in case it's used.
- Average is available: when aggregate_metric_double has the sum and value_count then we calculate the average. This is used in scripts, sorting and filtering queries.

Finally, we return no results in filtering, if none of the above apply.

Part of elastic#128356
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) >enhancement :StorageEngine/Mapping The storage related side of mappings Team:StorageEngine v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants