Skip to content

Commit 4f64167

Browse files
russcamMpdreamz
authored andcommitted
Sum on stats aggregation always returns a value (#3834)
Relates: elastic/elasticsearch#27193 This commit updates Sum property on StatsAggregate to be modelled as non-nullable and to derive ExtendedStatsAggregate from StatsAggregate. Adds integration test to assert deserialization behaviour.
1 parent 21b958d commit 4f64167

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

src/Nest/Aggregations/AggregateFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ private IAggregate GetStatsAggregate(ref JsonReader reader)
416416
reader.ReadNext(); // ,
417417
reader.ReadNext(); // sum
418418
reader.ReadNext(); // :
419-
var sum = reader.ReadNullableDouble();
419+
var sum = reader.ReadDouble();
420420

421421
var statsMetric = new StatsAggregate
422422
{

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
namespace Nest
22
{
3-
public class ExtendedStatsAggregate : MetricAggregateBase
3+
public class ExtendedStatsAggregate : StatsAggregate
44
{
5-
public double? Average { get; set; }
6-
public long Count { get; set; }
7-
public double? Max { get; set; }
8-
public double? Min { get; set; }
95
public double? StdDeviation { get; set; }
106
public StandardDeviationBounds StdDeviationBounds { get; set; }
11-
public double? Sum { get; set; }
127
public double? SumOfSquares { get; set; }
138
public double? Variance { get; set; }
149
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ public class StatsAggregate : MetricAggregateBase
66
public long Count { get; set; }
77
public double? Max { get; set; }
88
public double? Min { get; set; }
9-
public double? Sum { get; set; }
9+
public double Sum { get; set; }
1010
}
1111
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,71 @@ protected override void ExpectResponse(SearchResponse<Project> response)
5454
commitStats.StdDeviationBounds.Lower.Should().NotBe(0);
5555
}
5656
}
57+
58+
/// <summary>
59+
/// Asserts that stats sum is 0 (and not null) when matching document count is 0
60+
/// </summary>
61+
// hide
62+
public class ExtendedStatsAggregationUsageDocCountZeroTests : AggregationUsageTestBase
63+
{
64+
public ExtendedStatsAggregationUsageDocCountZeroTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
65+
66+
// a query that no docs will match
67+
protected override QueryContainer QueryScope => base.QueryScope &&
68+
new TermQuery { Field = Field<Project>(f => f.Branches), Value = "non-existent branch name" };
69+
70+
protected override object QueryScopeJson { get; } = new
71+
{
72+
@bool = new
73+
{
74+
must = new object[]
75+
{
76+
new { term = new { type = new { value = Project.TypeName } } },
77+
new { term = new { branches = new { value = "non-existent branch name" } } },
78+
}
79+
}
80+
81+
};
82+
83+
protected override object AggregationJson => new
84+
{
85+
commit_stats = new
86+
{
87+
extended_stats = new
88+
{
89+
field = "numberOfCommits",
90+
sigma = 1d
91+
}
92+
}
93+
};
94+
95+
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a
96+
.ExtendedStats("commit_stats", es => es
97+
.Field(p => p.NumberOfCommits)
98+
.Sigma(1)
99+
);
100+
101+
protected override AggregationDictionary InitializerAggs =>
102+
new ExtendedStatsAggregation("commit_stats", Field<Project>(p => p.NumberOfCommits))
103+
{
104+
Sigma = 1
105+
};
106+
107+
protected override void ExpectResponse(SearchResponse<Project> response)
108+
{
109+
response.ShouldBeValid();
110+
var commitStats = response.Aggregations.ExtendedStats("commit_stats");
111+
commitStats.Count.Should().Be(0);
112+
commitStats.Sum.Should().Be(0);
113+
commitStats.Should().NotBeNull();
114+
commitStats.Average.Should().BeNull();
115+
commitStats.Max.Should().BeNull();
116+
commitStats.Min.Should().BeNull();
117+
commitStats.SumOfSquares.Should().BeNull();
118+
commitStats.Variance.Should().BeNull();
119+
commitStats.StdDeviation.Should().BeNull();
120+
commitStats.StdDeviationBounds.Upper.Should().BeNull();
121+
commitStats.StdDeviationBounds.Lower.Should().BeNull();
122+
}
123+
}
57124
}

0 commit comments

Comments
 (0)