-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cumulative Cardinality agg (and Data Science plugin)
Addresses elastic/elasticsearch#43661
- Loading branch information
Showing
9 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Nest/Aggregations/Pipeline/CumulativeCardinality/CumulativeCardinalityAggregation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Elasticsearch.Net.Utf8Json; | ||
|
||
namespace Nest | ||
{ | ||
[InterfaceDataContract] | ||
[ReadAs(typeof(CumulativeCardinalityAggregation))] | ||
public interface ICumulativeCardinalityAggregation : IPipelineAggregation { } | ||
|
||
public class CumulativeCardinalityAggregation | ||
: PipelineAggregationBase, ICumulativeCardinalityAggregation | ||
{ | ||
internal CumulativeCardinalityAggregation() { } | ||
|
||
public CumulativeCardinalityAggregation(string name, SingleBucketsPath bucketsPath) | ||
: base(name, bucketsPath) { } | ||
|
||
internal override void WrapInContainer(AggregationContainer c) => c.CumulativeCardinality = this; | ||
} | ||
|
||
public class CumulativeCardinalityAggregationDescriptor | ||
: PipelineAggregationDescriptorBase<CumulativeCardinalityAggregationDescriptor, ICumulativeCardinalityAggregation, SingleBucketsPath> | ||
, ICumulativeCardinalityAggregation { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...Aggregations/Pipeline/CumulativeCardinality/CumulativeCardinalityAggregationUsageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Core.Extensions; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Domain; | ||
using Tests.Framework.EndpointTests.TestState; | ||
|
||
namespace Tests.Aggregations.Pipeline.CumulativeCardinality | ||
{ | ||
public class CumulativeCardinalityAggregationUsageTests : AggregationUsageTestBase | ||
{ | ||
public CumulativeCardinalityAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } | ||
|
||
protected override object AggregationJson => new | ||
{ | ||
projects_started_per_month = new | ||
{ | ||
date_histogram = new | ||
{ | ||
field = "startedOn", | ||
calendar_interval = "month", | ||
}, | ||
aggs = new | ||
{ | ||
commits = new | ||
{ | ||
cardinality = new | ||
{ | ||
field = "numberOfCommits" | ||
} | ||
}, | ||
cumulative_commits = new | ||
{ | ||
cumulative_cardinality = new | ||
{ | ||
buckets_path = "commits" | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a | ||
.DateHistogram("projects_started_per_month", dh => dh | ||
.Field(p => p.StartedOn) | ||
.CalendarInterval(DateInterval.Month) | ||
.Aggregations(aa => aa | ||
.Cardinality("commits", sm => sm | ||
.Field(p => p.NumberOfCommits) | ||
) | ||
.CumulativeCardinality("cumulative_commits", d => d | ||
.BucketsPath("commits") | ||
) | ||
) | ||
); | ||
|
||
protected override AggregationDictionary InitializerAggs => | ||
new DateHistogramAggregation("projects_started_per_month") | ||
{ | ||
Field = "startedOn", | ||
CalendarInterval = DateInterval.Month, | ||
Aggregations = | ||
new CardinalityAggregation("commits", "numberOfCommits") && | ||
new CumulativeCardinalityAggregation("cumulative_commits", "commits") | ||
}; | ||
|
||
protected override void ExpectResponse(ISearchResponse<Project> response) | ||
{ | ||
response.ShouldBeValid(); | ||
|
||
var projectsPerMonth = response.Aggregations.DateHistogram("projects_started_per_month"); | ||
projectsPerMonth.Should().NotBeNull(); | ||
projectsPerMonth.Buckets.Should().NotBeNull(); | ||
projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0); | ||
|
||
foreach (var item in projectsPerMonth.Buckets) | ||
{ | ||
var commitsDerivative = item.CumulativeCardinality("cumulative_commits"); | ||
commitsDerivative.Should().NotBeNull(); | ||
commitsDerivative.Value.Should().NotBe(null); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters