From d565147161c3d6e8d2f67fb6371466ccebbe8532 Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Fri, 31 Jul 2020 11:27:38 +1000 Subject: [PATCH] Add standard deviation / variance sampling to extended stats (#4891) Relates: elastic/elasticsearch#49782 --- src/Nest/Aggregations/AggregateFormatter.cs | 18 +++++- .../ExtendedStats/ExtendedStatsAggregate.cs | 55 +++++++++++++++++++ .../ExtendedStatsAggregationUsageTests.cs | 16 +++++- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/Nest/Aggregations/AggregateFormatter.cs b/src/Nest/Aggregations/AggregateFormatter.cs index 0ffe5221662..535ab378d16 100644 --- a/src/Nest/Aggregations/AggregateFormatter.cs +++ b/src/Nest/Aggregations/AggregateFormatter.cs @@ -72,7 +72,11 @@ internal class AggregateFormatter : IJsonFormatter { { "variance", 0 }, { "std_deviation", 1 }, - { "std_deviation_bounds", 2 } + { "std_deviation_bounds", 2 }, + { "variance_population", 3 }, + { "variance_sampling", 4 }, + { "std_deviation_population", 5 }, + { "std_deviation_sampling", 6 }, }; private static readonly byte[] ValueAsStringField = JsonWriter.GetEncodedPropertyNameWithoutQuotation(Parser.ValueAsString); @@ -589,6 +593,18 @@ private IAggregate GetExtendedStatsAggregate(ref JsonReader reader, IJsonFormatt extendedStatsMetric.StdDeviationBounds = formatterResolver.GetFormatter().Deserialize(ref reader, formatterResolver); break; + case 3: + extendedStatsMetric.VariancePopulation = reader.ReadNullableDouble(); + break; + case 4: + extendedStatsMetric.VarianceSampling = reader.ReadNullableDouble(); + break; + case 5: + extendedStatsMetric.StdDeviationPopulation = reader.ReadNullableDouble(); + break; + case 6: + extendedStatsMetric.StdDeviationSampling = reader.ReadNullableDouble(); + break; } } else diff --git a/src/Nest/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregate.cs b/src/Nest/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregate.cs index 4dc220f8fec..fd0d096e3e5 100644 --- a/src/Nest/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregate.cs +++ b/src/Nest/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregate.cs @@ -8,10 +8,53 @@ namespace Nest { public class ExtendedStatsAggregate : StatsAggregate { + /// + /// The standard deviation of the collected values + /// public double? StdDeviation { get; set; } + + /// + /// The upper or lower bounds of standard deviation + /// public StandardDeviationBounds StdDeviationBounds { get; set; } + + /// + /// The sum of squares of the collected values + /// public double? SumOfSquares { get; set; } + + /// + /// The variance of the collected values + /// public double? Variance { get; set; } + + /// + /// The population variance of the collected values. + /// + /// Valid in Elasticsearch 7.9.0+ + /// + public double? VariancePopulation { get; set; } + + /// + /// The sampling variance of the collected values. + /// + /// Valid in Elasticsearch 7.9.0+ + /// + public double? VarianceSampling { get; set; } + + /// + /// The population standard deviation of the collected values. + /// + /// Valid in Elasticsearch 7.9.0+ + /// + public double? StdDeviationPopulation { get; set; } + + /// + /// The sampling standard deviation of the collected values. + /// + /// Valid in Elasticsearch 7.9.0+ + /// + public double? StdDeviationSampling { get; set; } } [DataContract] @@ -22,5 +65,17 @@ public class StandardDeviationBounds [DataMember(Name = "upper")] public double? Upper { get; set; } + + [DataMember(Name = "lower_population")] + public double? LowerPopulation { get; set; } + + [DataMember(Name = "upper_population")] + public double? UpperPopulation { get; set; } + + [DataMember(Name = "lower_sampling")] + public double? LowerSampling { get; set; } + + [DataMember(Name = "upper_sampling")] + public double? UpperSampling { get; set; } } } diff --git a/tests/Tests/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregationUsageTests.cs b/tests/Tests/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregationUsageTests.cs index bb49428a682..8bee5a81d82 100644 --- a/tests/Tests/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregationUsageTests.cs +++ b/tests/Tests/Aggregations/Metric/ExtendedStats/ExtendedStatsAggregationUsageTests.cs @@ -2,9 +2,10 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -using System; +using System; using FluentAssertions; using Nest; +using Tests.Core.Client; using Tests.Core.Extensions; using Tests.Core.ManagedElasticsearch.Clusters; using Tests.Domain; @@ -56,6 +57,19 @@ protected override void ExpectResponse(ISearchResponse response) commitStats.StdDeviationBounds.Should().NotBeNull(); commitStats.StdDeviationBounds.Upper.Should().BeGreaterThan(0); commitStats.StdDeviationBounds.Lower.Should().NotBe(0); + + // hide + if (TestClient.Configuration.InRange(">=7.9.0")) + { + commitStats.VariancePopulation.Should().BeGreaterThan(0); + commitStats.VarianceSampling.Should().BeGreaterThan(0); + commitStats.StdDeviationPopulation.Should().BeGreaterThan(0); + commitStats.StdDeviationSampling.Should().BeGreaterThan(0); + commitStats.StdDeviationBounds.UpperPopulation.Should().BeGreaterThan(0); + commitStats.StdDeviationBounds.UpperSampling.Should().NotBe(0); + commitStats.StdDeviationBounds.LowerPopulation.Should().NotBe(0); + commitStats.StdDeviationBounds.LowerSampling.Should().NotBe(0); + } } }