Skip to content

Refactor Metric aggregations #4332

Closed
Closed
@russcam

Description

@russcam

As part of #4331, it looks like not all IMetricAggregation implementations support scripting i.e. the Script property

Going through the aggregation builder types in Elasticsearch, whether an aggregation supports scripting, formatting, etc. is controlled by declaring the supported fields on the parser. For example, for MinAggregationBuilder

https://github.com/elastic/elasticsearch/blob/17358b5af7b34a2b9a15846ba672f5a04001fcd5/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregationBuilder.java#L48

public class MinAggregationBuilder extends ValuesSourceAggregationBuilder.LeafOnly<ValuesSource.Numeric, MinAggregationBuilder> {
    public static final String NAME = "min";

    private static final ObjectParser<MinAggregationBuilder, Void> PARSER;
    static {
        PARSER = new ObjectParser<>(AvgAggregationBuilder.NAME);
        ValuesSourceParserHelper.declareNumericFields(PARSER, true, true, false); <---
    }

   ...

where the bool values are scriptable, formattable and timezoneAware, respectively.

Only the IMetricAggregation that support scripting should allow a Script property to be set. Going through the 7.x Java source, this is the support found

agg name scriptable formattable
avg ✔️ ✔️
cardinality ✔️
extended_stats ✔️ ✔️
geo_centroid ✔️
max ✔️ ✔️
median_absolute_derivation ✔️ ✔️
min ✔️ ✔️
percentile_ranks ✔️ ✔️
percentiles ✔️ ✔️
stats ✔️ ✔️
sum ✔️ ✔️
value_count ✔️ ✔️

(timezone aware is not a column because it doesn't look to apply to metric aggs)
(metric aggregations omitted from the list do not support either script or format)

Possible implementation

Derive an IScriptableMetricAggregation from IMetricAggregation, and IFormattableMetricAggregation from IScriptableMetricAggregation

	public interface IMetricAggregation : IAggregation
	{
		Field Field { get; set; }

		[DataMember(Name ="missing")]
		object Missing { get; set; }
	}

	public interface IScriptableMetricAggregation : IMetricAggregation 
	{
		[DataMember(Name ="script")]
		IScript Script { get; set; }
	}

	public interface IFormattableMetricAggregation : IScriptableMetricAggregation 
	{
		[DataMember(Name ="format")]
		stringFormat { get; set; }
	}

NOTE: Missing should also be changed to object

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions