Skip to content

Commit c55aed2

Browse files
committed
Add standard deviation / variance sampling to extended stats
Relates: elastic/elasticsearch#49782
1 parent bb4846e commit c55aed2

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

src/Nest/Aggregations/AggregateFormatter.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ internal class AggregateFormatter : IJsonFormatter<IAggregate>
7272
{
7373
{ "variance", 0 },
7474
{ "std_deviation", 1 },
75-
{ "std_deviation_bounds", 2 }
75+
{ "std_deviation_bounds", 2 },
76+
{ "variance_population", 3 },
77+
{ "variance_sampling", 4 },
78+
{ "std_deviation_population", 5 },
79+
{ "std_deviation_sampling", 6 },
7680
};
7781

7882
private static readonly byte[] ValueAsStringField = JsonWriter.GetEncodedPropertyNameWithoutQuotation(Parser.ValueAsString);
@@ -589,6 +593,18 @@ private IAggregate GetExtendedStatsAggregate(ref JsonReader reader, IJsonFormatt
589593
extendedStatsMetric.StdDeviationBounds =
590594
formatterResolver.GetFormatter<StandardDeviationBounds>().Deserialize(ref reader, formatterResolver);
591595
break;
596+
case 3:
597+
extendedStatsMetric.VariancePopulation = reader.ReadNullableDouble();
598+
break;
599+
case 4:
600+
extendedStatsMetric.VarianceSampling = reader.ReadNullableDouble();
601+
break;
602+
case 5:
603+
extendedStatsMetric.StdDeviationPopulation = reader.ReadNullableDouble();
604+
break;
605+
case 6:
606+
extendedStatsMetric.StdDeviationSampling = reader.ReadNullableDouble();
607+
break;
592608
}
593609
}
594610
else

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,53 @@ namespace Nest
88
{
99
public class ExtendedStatsAggregate : StatsAggregate
1010
{
11+
/// <summary>
12+
/// The standard deviation of the collected values
13+
/// </summary>
1114
public double? StdDeviation { get; set; }
15+
16+
/// <summary>
17+
/// The upper or lower bounds of standard deviation
18+
/// </summary>
1219
public StandardDeviationBounds StdDeviationBounds { get; set; }
20+
21+
/// <summary>
22+
/// The sum of squares of the collected values
23+
/// </summary>
1324
public double? SumOfSquares { get; set; }
25+
26+
/// <summary>
27+
/// The variance of the collected values
28+
/// </summary>
1429
public double? Variance { get; set; }
30+
31+
/// <summary>
32+
/// The population variance of the collected values.
33+
/// <para />
34+
/// Valid in Elasticsearch 7.9.0+
35+
/// </summary>
36+
public double? VariancePopulation { get; set; }
37+
38+
/// <summary>
39+
/// The sampling variance of the collected values.
40+
/// <para />
41+
/// Valid in Elasticsearch 7.9.0+
42+
/// </summary>
43+
public double? VarianceSampling { get; set; }
44+
45+
/// <summary>
46+
/// The population standard deviation of the collected values.
47+
/// <para />
48+
/// Valid in Elasticsearch 7.9.0+
49+
/// </summary>
50+
public double? StdDeviationPopulation { get; set; }
51+
52+
/// <summary>
53+
/// The sampling standard deviation of the collected values.
54+
/// <para />
55+
/// Valid in Elasticsearch 7.9.0+
56+
/// </summary>
57+
public double? StdDeviationSampling { get; set; }
1558
}
1659

1760
[DataContract]
@@ -22,5 +65,17 @@ public class StandardDeviationBounds
2265

2366
[DataMember(Name = "upper")]
2467
public double? Upper { get; set; }
68+
69+
[DataMember(Name = "lower_population")]
70+
public double? LowerPopulation { get; set; }
71+
72+
[DataMember(Name = "upper_population")]
73+
public double? UpperPopulation { get; set; }
74+
75+
[DataMember(Name = "lower_sampling")]
76+
public double? LowerSampling { get; set; }
77+
78+
[DataMember(Name = "upper_sampling")]
79+
public double? UpperSampling { get; set; }
2580
}
2681
}

tests/Tests/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregationUsageTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
using System;
5+
using System;
66
using FluentAssertions;
77
using Nest;
8+
using Tests.Core.Client;
89
using Tests.Core.Extensions;
910
using Tests.Core.ManagedElasticsearch.Clusters;
1011
using Tests.Domain;
@@ -56,6 +57,19 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
5657
commitStats.StdDeviationBounds.Should().NotBeNull();
5758
commitStats.StdDeviationBounds.Upper.Should().BeGreaterThan(0);
5859
commitStats.StdDeviationBounds.Lower.Should().NotBe(0);
60+
61+
// hide
62+
if (TestClient.Configuration.InRange(">=7.9.0"))
63+
{
64+
commitStats.VariancePopulation.Should().BeGreaterThan(0);
65+
commitStats.VarianceSampling.Should().BeGreaterThan(0);
66+
commitStats.StdDeviationPopulation.Should().BeGreaterThan(0);
67+
commitStats.StdDeviationSampling.Should().BeGreaterThan(0);
68+
commitStats.StdDeviationBounds.UpperPopulation.Should().BeGreaterThan(0);
69+
commitStats.StdDeviationBounds.UpperSampling.Should().NotBe(0);
70+
commitStats.StdDeviationBounds.LowerPopulation.Should().NotBe(0);
71+
commitStats.StdDeviationBounds.LowerSampling.Should().NotBe(0);
72+
}
5973
}
6074
}
6175

0 commit comments

Comments
 (0)