Skip to content

Sum on stats aggregation always returns a value #3834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Nest/Aggregations/AggregateFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ private IAggregate GetStatsAggregate(ref JsonReader reader)
reader.ReadNext(); // ,
reader.ReadNext(); // sum
reader.ReadNext(); // :
var sum = reader.ReadNullableDouble();
var sum = reader.ReadDouble();

var statsMetric = new StatsAggregate
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
namespace Nest
{
public class ExtendedStatsAggregate : MetricAggregateBase
public class ExtendedStatsAggregate : StatsAggregate
{
public double? Average { get; set; }
public long Count { get; set; }
public double? Max { get; set; }
public double? Min { get; set; }
public double? StdDeviation { get; set; }
public StandardDeviationBounds StdDeviationBounds { get; set; }
public double? Sum { get; set; }
public double? SumOfSquares { get; set; }
public double? Variance { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nest/Aggregations/Metric/Stats/StatsAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class StatsAggregate : MetricAggregateBase
public long Count { get; set; }
public double? Max { get; set; }
public double? Min { get; set; }
public double? Sum { get; set; }
public double Sum { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,71 @@ protected override void ExpectResponse(SearchResponse<Project> response)
commitStats.StdDeviationBounds.Lower.Should().NotBe(0);
}
}

/// <summary>
/// Asserts that stats sum is 0 (and not null) when matching document count is 0
/// </summary>
// hide
public class ExtendedStatsAggregationUsageDocCountZeroTests : AggregationUsageTestBase
{
public ExtendedStatsAggregationUsageDocCountZeroTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }

// a query that no docs will match
protected override QueryContainer QueryScope => base.QueryScope &&
new TermQuery { Field = Field<Project>(f => f.Branches), Value = "non-existent branch name" };

protected override object QueryScopeJson { get; } = new
{
@bool = new
{
must = new object[]
{
new { term = new { type = new { value = Project.TypeName } } },
new { term = new { branches = new { value = "non-existent branch name" } } },
}
}

};

protected override object AggregationJson => new
{
commit_stats = new
{
extended_stats = new
{
field = "numberOfCommits",
sigma = 1d
}
}
};

protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a
.ExtendedStats("commit_stats", es => es
.Field(p => p.NumberOfCommits)
.Sigma(1)
);

protected override AggregationDictionary InitializerAggs =>
new ExtendedStatsAggregation("commit_stats", Field<Project>(p => p.NumberOfCommits))
{
Sigma = 1
};

protected override void ExpectResponse(SearchResponse<Project> response)
{
response.ShouldBeValid();
var commitStats = response.Aggregations.ExtendedStats("commit_stats");
commitStats.Count.Should().Be(0);
commitStats.Sum.Should().Be(0);
commitStats.Should().NotBeNull();
commitStats.Average.Should().BeNull();
commitStats.Max.Should().BeNull();
commitStats.Min.Should().BeNull();
commitStats.SumOfSquares.Should().BeNull();
commitStats.Variance.Should().BeNull();
commitStats.StdDeviation.Should().BeNull();
commitStats.StdDeviationBounds.Upper.Should().BeNull();
commitStats.StdDeviationBounds.Lower.Should().BeNull();
}
}
}