Skip to content

Commit bd51ef1

Browse files
committed
Add format to metric aggregations that support it (#4331)
This commit adds a Format property to metrics aggregations that support it. Closes #4327 (cherry picked from commit 0576da2)
1 parent 6c7e056 commit bd51ef1

File tree

15 files changed

+122
-35
lines changed

15 files changed

+122
-35
lines changed

src/Nest/Aggregations/Aggregation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Nest
66
{
77
/// <summary>
8-
/// Represents an aggregation on the request
8+
/// An aggregation on the request
99
/// </summary>
1010
[InterfaceDataContract]
1111
public interface IAggregation

src/Nest/Aggregations/Metric/Average/AverageAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Nest
44
{
55
[InterfaceDataContract]
66
[ReadAs(typeof(AverageAggregation))]
7-
public interface IAverageAggregation : IMetricAggregation { }
7+
public interface IAverageAggregation : IFormattableMetricAggregation { }
88

9-
public class AverageAggregation : MetricAggregationBase, IAverageAggregation
9+
public class AverageAggregation : FormattableMetricAggregationBase, IAverageAggregation
1010
{
1111
internal AverageAggregation() { }
1212

@@ -16,7 +16,7 @@ public AverageAggregation(string name, Field field) : base(name, field) { }
1616
}
1717

1818
public class AverageAggregationDescriptor<T>
19-
: MetricAggregationDescriptorBase<AverageAggregationDescriptor<T>, IAverageAggregation, T>
19+
: FormattableMetricAggregationDescriptorBase<AverageAggregationDescriptor<T>, IAverageAggregation, T>
2020
, IAverageAggregation
2121
where T : class { }
2222
}

src/Nest/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace Nest
55
{
66
[InterfaceDataContract]
77
[ReadAs(typeof(ExtendedStatsAggregation))]
8-
public interface IExtendedStatsAggregation : IMetricAggregation
8+
public interface IExtendedStatsAggregation : IFormattableMetricAggregation
99
{
1010
[DataMember(Name ="sigma")]
1111
double? Sigma { get; set; }
1212
}
1313

14-
public class ExtendedStatsAggregation : MetricAggregationBase, IExtendedStatsAggregation
14+
public class ExtendedStatsAggregation : FormattableMetricAggregationBase, IExtendedStatsAggregation
1515
{
1616
internal ExtendedStatsAggregation() { }
1717

@@ -23,7 +23,7 @@ public ExtendedStatsAggregation(string name, Field field) : base(name, field) {
2323
}
2424

2525
public class ExtendedStatsAggregationDescriptor<T>
26-
: MetricAggregationDescriptorBase<ExtendedStatsAggregationDescriptor<T>, IExtendedStatsAggregation, T>
26+
: FormattableMetricAggregationDescriptorBase<ExtendedStatsAggregationDescriptor<T>, IExtendedStatsAggregation, T>
2727
, IExtendedStatsAggregation
2828
where T : class
2929
{

src/Nest/Aggregations/Metric/Max/MaxAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Nest
44
{
55
[InterfaceDataContract]
66
[ReadAs(typeof(MaxAggregation))]
7-
public interface IMaxAggregation : IMetricAggregation { }
7+
public interface IMaxAggregation : IFormattableMetricAggregation { }
88

9-
public class MaxAggregation : MetricAggregationBase, IMaxAggregation
9+
public class MaxAggregation : FormattableMetricAggregationBase, IMaxAggregation
1010
{
1111
internal MaxAggregation() { }
1212

@@ -16,7 +16,7 @@ public MaxAggregation(string name, Field field) : base(name, field) { }
1616
}
1717

1818
public class MaxAggregationDescriptor<T>
19-
: MetricAggregationDescriptorBase<MaxAggregationDescriptor<T>, IMaxAggregation, T>
19+
: FormattableMetricAggregationDescriptorBase<MaxAggregationDescriptor<T>, IMaxAggregation, T>
2020
, IMaxAggregation
2121
where T : class { }
2222
}

src/Nest/Aggregations/Metric/MedianAbsoluteDeviation/MedianAbsoluteDeviationAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Nest
1616
/// </summary>
1717
[InterfaceDataContract]
1818
[ReadAs(typeof(MedianAbsoluteDeviationAggregation))]
19-
public interface IMedianAbsoluteDeviationAggregation : IMetricAggregation
19+
public interface IMedianAbsoluteDeviationAggregation : IFormattableMetricAggregation
2020
{
2121
/// <summary>
2222
/// TDigest algorithm component that controls memory usage and approximation error.
@@ -30,7 +30,7 @@ public interface IMedianAbsoluteDeviationAggregation : IMetricAggregation
3030
}
3131

3232
/// <inheritdoc cref="IMedianAbsoluteDeviationAggregation"/>
33-
public class MedianAbsoluteDeviationAggregation : MetricAggregationBase, IMedianAbsoluteDeviationAggregation
33+
public class MedianAbsoluteDeviationAggregation : FormattableMetricAggregationBase, IMedianAbsoluteDeviationAggregation
3434
{
3535
internal MedianAbsoluteDeviationAggregation() { }
3636

@@ -44,7 +44,7 @@ public MedianAbsoluteDeviationAggregation(string name, Field field) : base(name,
4444

4545
/// <inheritdoc cref="IMedianAbsoluteDeviationAggregation"/>
4646
public class MedianAbsoluteDeviationAggregationDescriptor<T>
47-
: MetricAggregationDescriptorBase<MedianAbsoluteDeviationAggregationDescriptor<T>, IMedianAbsoluteDeviationAggregation, T>
47+
: FormattableMetricAggregationDescriptorBase<MedianAbsoluteDeviationAggregationDescriptor<T>, IMedianAbsoluteDeviationAggregation, T>
4848
, IMedianAbsoluteDeviationAggregation
4949
where T : class
5050
{

src/Nest/Aggregations/Metric/MetricAggregation.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,44 @@
55

66
namespace Nest
77
{
8+
/// <summary>
9+
/// An aggregation that computes metrics based on values extracted in
10+
/// one way or another from the documents that are being aggregated.
11+
/// </summary>
812
public interface IMetricAggregation : IAggregation
913
{
14+
/// <summary>
15+
/// The field on which to aggregate
16+
/// </summary>
1017
[DataMember(Name ="field")]
1118
Field Field { get; set; }
1219

20+
/// <summary>
21+
/// The value to use when the aggregation finds a missing value in a document
22+
/// </summary>
23+
// TODO: This should be object in 8.x
1324
[DataMember(Name ="missing")]
1425
double? Missing { get; set; }
1526

27+
/// <summary>
28+
/// The script to use for the aggregation
29+
/// </summary>
1630
[DataMember(Name ="script")]
1731
IScript Script { get; set; }
1832
}
1933

34+
/// <summary>
35+
/// A metric aggregation that can accept a format to use for the output of the aggregation
36+
/// </summary>
37+
public interface IFormattableMetricAggregation : IMetricAggregation
38+
{
39+
/// <summary>
40+
/// The format to use for the output of the aggregation
41+
/// </summary>
42+
[DataMember(Name = "format")]
43+
string Format { get; set; }
44+
}
45+
2046
public abstract class MetricAggregationBase : AggregationBase, IMetricAggregation
2147
{
2248
internal MetricAggregationBase() { }
@@ -28,6 +54,16 @@ internal MetricAggregationBase() { }
2854
public virtual IScript Script { get; set; }
2955
}
3056

57+
public abstract class FormattableMetricAggregationBase : MetricAggregationBase, IFormattableMetricAggregation
58+
{
59+
internal FormattableMetricAggregationBase() { }
60+
61+
protected FormattableMetricAggregationBase(string name, Field field) : base(name, field) { }
62+
63+
/// <inheritdoc />
64+
public string Format { get; set; }
65+
}
66+
3167
public abstract class MetricAggregationDescriptorBase<TMetricAggregation, TMetricAggregationInterface, T>
3268
: DescriptorBase<TMetricAggregation, TMetricAggregationInterface>, IMetricAggregation
3369
where TMetricAggregation : MetricAggregationDescriptorBase<TMetricAggregation, TMetricAggregationInterface, T>
@@ -45,18 +81,39 @@ public abstract class MetricAggregationDescriptorBase<TMetricAggregation, TMetri
4581

4682
IScript IMetricAggregation.Script { get; set; }
4783

84+
/// <inheritdoc cref="IMetricAggregation.Field"/>
4885
public TMetricAggregation Field(Field field) => Assign(field, (a, v) => a.Field = v);
4986

87+
/// <inheritdoc cref="IMetricAggregation.Field"/>
5088
public TMetricAggregation Field<TValue>(Expression<Func<T, TValue>> field) => Assign(field, (a, v) => a.Field = v);
5189

90+
/// <inheritdoc cref="IMetricAggregation.Script"/>
5291
public virtual TMetricAggregation Script(string script) => Assign((InlineScript)script, (a, v) => a.Script = v);
5392

93+
/// <inheritdoc cref="IMetricAggregation.Script"/>
5494
public virtual TMetricAggregation Script(Func<ScriptDescriptor, IScript> scriptSelector) =>
5595
Assign(scriptSelector, (a, v) => a.Script = v?.Invoke(new ScriptDescriptor()));
5696

97+
/// <inheritdoc cref="IMetricAggregation.Missing"/>
5798
public TMetricAggregation Missing(double? missing) => Assign(missing, (a, v) => a.Missing = v);
5899

100+
/// <inheritdoc cref="IAggregation.Meta"/>
59101
public TMetricAggregation Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) =>
60102
Assign(selector, (a, v) => a.Meta = v?.Invoke(new FluentDictionary<string, object>()));
61103
}
104+
105+
public abstract class FormattableMetricAggregationDescriptorBase<TFormattableMetricAggregation, TFormattableMetricAggregationInterface, T>
106+
: MetricAggregationDescriptorBase<TFormattableMetricAggregation, TFormattableMetricAggregationInterface, T>, IFormattableMetricAggregation
107+
where TFormattableMetricAggregation :
108+
FormattableMetricAggregationDescriptorBase<TFormattableMetricAggregation, TFormattableMetricAggregationInterface, T>
109+
, TFormattableMetricAggregationInterface, IFormattableMetricAggregation
110+
where T : class
111+
where TFormattableMetricAggregationInterface : class, IFormattableMetricAggregation
112+
{
113+
string IFormattableMetricAggregation.Format { get; set; }
114+
115+
/// <inheritdoc cref="IFormattableMetricAggregation.Format"/>
116+
public TFormattableMetricAggregation Format(string format) =>
117+
Assign(format, (a, v) => a.Format = v);
118+
}
62119
}

src/Nest/Aggregations/Metric/Min/MinAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Nest
44
{
55
[InterfaceDataContract]
66
[ReadAs(typeof(MinAggregation))]
7-
public interface IMinAggregation : IMetricAggregation { }
7+
public interface IMinAggregation : IFormattableMetricAggregation { }
88

9-
public class MinAggregation : MetricAggregationBase, IMinAggregation
9+
public class MinAggregation : FormattableMetricAggregationBase, IMinAggregation
1010
{
1111
internal MinAggregation() { }
1212

@@ -16,7 +16,7 @@ public MinAggregation(string name, Field field) : base(name, field) { }
1616
}
1717

1818
public class MinAggregationDescriptor<T>
19-
: MetricAggregationDescriptorBase<MinAggregationDescriptor<T>, IMinAggregation, T>
19+
: FormattableMetricAggregationDescriptorBase<MinAggregationDescriptor<T>, IMinAggregation, T>
2020
, IMinAggregation
2121
where T : class { }
2222
}

src/Nest/Aggregations/Metric/PercentileRanks/PercentileRanksAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace Nest
66
{
77
[JsonFormatter(typeof(PercentileRanksAggregationFormatter))]
8-
public interface IPercentileRanksAggregation : IMetricAggregation
8+
public interface IPercentileRanksAggregation : IFormattableMetricAggregation
99
{
1010
IPercentilesMethod Method { get; set; }
1111
IEnumerable<double> Values { get; set; }
1212
bool? Keyed { get; set; }
1313
}
1414

15-
public class PercentileRanksAggregation : MetricAggregationBase, IPercentileRanksAggregation
15+
public class PercentileRanksAggregation : FormattableMetricAggregationBase, IPercentileRanksAggregation
1616
{
1717
internal PercentileRanksAggregation() { }
1818

@@ -26,7 +26,7 @@ public PercentileRanksAggregation(string name, Field field) : base(name, field)
2626
}
2727

2828
public class PercentileRanksAggregationDescriptor<T>
29-
: MetricAggregationDescriptorBase<PercentileRanksAggregationDescriptor<T>, IPercentileRanksAggregation, T>, IPercentileRanksAggregation
29+
: FormattableMetricAggregationDescriptorBase<PercentileRanksAggregationDescriptor<T>, IPercentileRanksAggregation, T>, IPercentileRanksAggregation
3030
where T : class
3131
{
3232
IPercentilesMethod IPercentileRanksAggregation.Method { get; set; }

src/Nest/Aggregations/Metric/PercentileRanks/PercentileRanksAggregationFormatter.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ internal class PercentileRanksAggregationFormatter : IJsonFormatter<IPercentileR
1717
{ "missing", 4 },
1818
{ "meta", 5 },
1919
{ "values", 6 },
20-
{ "keyed", 7 }
20+
{ "keyed", 7 },
21+
{ "format", 8 }
2122
};
2223

2324
public IPercentileRanksAggregation Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
@@ -67,6 +68,9 @@ public IPercentileRanksAggregation Deserialize(ref JsonReader reader, IJsonForma
6768
case 7:
6869
percentiles.Keyed = reader.ReadBoolean();
6970
break;
71+
case 8:
72+
percentiles.Format = reader.ReadString();
73+
break;
7074
}
7175
}
7276
}
@@ -179,6 +183,16 @@ public void Serialize(ref JsonWriter writer, IPercentileRanksAggregation value,
179183

180184
writer.WritePropertyName("keyed");
181185
writer.WriteBoolean(value.Keyed.Value);
186+
propertyWritten = true;
187+
}
188+
189+
if (!string.IsNullOrEmpty(value.Format))
190+
{
191+
if (propertyWritten)
192+
writer.WriteValueSeparator();
193+
194+
writer.WritePropertyName("format");
195+
writer.WriteString(value.Format);
182196
}
183197

184198
writer.WriteEndObject();

src/Nest/Aggregations/Metric/Percentiles/PercentilesAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace Nest
66
{
77
[JsonFormatter(typeof(PercentilesAggregationFormatter))]
8-
public interface IPercentilesAggregation : IMetricAggregation
8+
public interface IPercentilesAggregation : IFormattableMetricAggregation
99
{
1010
IPercentilesMethod Method { get; set; }
1111
IEnumerable<double> Percents { get; set; }
1212
bool? Keyed { get; set; }
1313
}
1414

15-
public class PercentilesAggregation : MetricAggregationBase, IPercentilesAggregation
15+
public class PercentilesAggregation : FormattableMetricAggregationBase, IPercentilesAggregation
1616
{
1717
internal PercentilesAggregation() { }
1818

@@ -26,7 +26,7 @@ public PercentilesAggregation(string name, Field field) : base(name, field) { }
2626
}
2727

2828
public class PercentilesAggregationDescriptor<T>
29-
: MetricAggregationDescriptorBase<PercentilesAggregationDescriptor<T>, IPercentilesAggregation, T>
29+
: FormattableMetricAggregationDescriptorBase<PercentilesAggregationDescriptor<T>, IPercentilesAggregation, T>
3030
, IPercentilesAggregation
3131
where T : class
3232
{

src/Nest/Aggregations/Metric/Percentiles/PercentilesAggregationFormatter.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ internal class PercentilesAggregationFormatter : IJsonFormatter<IPercentilesAggr
1717
{ "missing", 4 },
1818
{ "percents", 5 },
1919
{ "meta", 6 },
20-
{ "keyed", 7 }
20+
{ "keyed", 7 },
21+
{ "format", 8 },
2122
};
2223

2324
public IPercentilesAggregation Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
@@ -67,6 +68,9 @@ public IPercentilesAggregation Deserialize(ref JsonReader reader, IJsonFormatter
6768
case 7:
6869
percentiles.Keyed = reader.ReadBoolean();
6970
break;
71+
case 8:
72+
percentiles.Format = reader.ReadString();
73+
break;
7074
}
7175
}
7276
}
@@ -179,6 +183,16 @@ public void Serialize(ref JsonWriter writer, IPercentilesAggregation value, IJso
179183

180184
writer.WritePropertyName("keyed");
181185
writer.WriteBoolean(value.Keyed.Value);
186+
propertyWritten = true;
187+
}
188+
189+
if (!string.IsNullOrEmpty(value.Format))
190+
{
191+
if (propertyWritten)
192+
writer.WriteValueSeparator();
193+
194+
writer.WritePropertyName("format");
195+
writer.WriteString(value.Format);
182196
}
183197

184198
writer.WriteEndObject();

src/Nest/Aggregations/Metric/Stats/StatsAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Nest
44
{
55
[InterfaceDataContract]
66
[ReadAs(typeof(StatsAggregation))]
7-
public interface IStatsAggregation : IMetricAggregation { }
7+
public interface IStatsAggregation : IFormattableMetricAggregation { }
88

9-
public class StatsAggregation : MetricAggregationBase, IStatsAggregation
9+
public class StatsAggregation : FormattableMetricAggregationBase, IStatsAggregation
1010
{
1111
internal StatsAggregation() { }
1212

@@ -16,7 +16,7 @@ public StatsAggregation(string name, Field field) : base(name, field) { }
1616
}
1717

1818
public class StatsAggregationDescriptor<T>
19-
: MetricAggregationDescriptorBase<StatsAggregationDescriptor<T>, IStatsAggregation, T>
19+
: FormattableMetricAggregationDescriptorBase<StatsAggregationDescriptor<T>, IStatsAggregation, T>
2020
, IStatsAggregation
2121
where T : class { }
2222
}

src/Nest/Aggregations/Metric/Sum/SumAggregation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Nest
44
{
55
[InterfaceDataContract]
66
[ReadAs(typeof(SumAggregation))]
7-
public interface ISumAggregation : IMetricAggregation { }
7+
public interface ISumAggregation : IFormattableMetricAggregation { }
88

9-
public class SumAggregation : MetricAggregationBase, ISumAggregation
9+
public class SumAggregation : FormattableMetricAggregationBase, ISumAggregation
1010
{
1111
internal SumAggregation() { }
1212

@@ -16,7 +16,7 @@ public SumAggregation(string name, Field field) : base(name, field) { }
1616
}
1717

1818
public class SumAggregationDescriptor<T>
19-
: MetricAggregationDescriptorBase<SumAggregationDescriptor<T>, ISumAggregation, T>
19+
: FormattableMetricAggregationDescriptorBase<SumAggregationDescriptor<T>, ISumAggregation, T>
2020
, ISumAggregation
2121
where T : class { }
2222
}

0 commit comments

Comments
 (0)