Skip to content

Commit

Permalink
Add Cumulative Cardinality agg (and Data Science plugin)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Oct 21, 2019
1 parent a13e933 commit 3c70247
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Nest/Aggregations/AggregateDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ internal static string[] TypedKeyTokens(string key)

public ValueAggregate CumulativeSum(string key) => TryGet<ValueAggregate>(key);

public ValueAggregate CumulativeCardinality(string key) => TryGet<ValueAggregate>(key);

public ValueAggregate BucketScript(string key) => TryGet<ValueAggregate>(key);

public ValueAggregate SerialDifferencing(string key) => TryGet<ValueAggregate>(key);
Expand Down
13 changes: 13 additions & 0 deletions src/Nest/Aggregations/AggregationContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public interface IAggregationContainer
[DataMember(Name = "cumulative_sum")]
ICumulativeSumAggregation CumulativeSum { get; set; }

[DataMember(Name = "cumulative_cardinality")]
ICumulativeCardinalityAggregation CumulativeCardinality { get; set; }

[DataMember(Name = "date_histogram")]
IDateHistogramAggregation DateHistogram { get; set; }

Expand Down Expand Up @@ -282,6 +285,8 @@ public class AggregationContainer : IAggregationContainer

public ICumulativeSumAggregation CumulativeSum { get; set; }

public ICumulativeCardinalityAggregation CumulativeCardinality { get; set; }

public IDateHistogramAggregation DateHistogram { get; set; }

public IAutoDateHistogramAggregation AutoDateHistogram { get; set; }
Expand Down Expand Up @@ -358,6 +363,7 @@ public class AggregationContainer : IAggregationContainer
public IStatsAggregation Stats { get; set; }

public IStatsBucketAggregation StatsBucket { get; set; }

public ISumAggregation Sum { get; set; }

public ISumBucketAggregation SumBucket { get; set; }
Expand Down Expand Up @@ -425,6 +431,8 @@ public class AggregationContainerDescriptor<T> : DescriptorBase<AggregationConta

ICumulativeSumAggregation IAggregationContainer.CumulativeSum { get; set; }

ICumulativeCardinalityAggregation IAggregationContainer.CumulativeCardinality { get; set; }

IDateHistogramAggregation IAggregationContainer.DateHistogram { get; set; }

IAutoDateHistogramAggregation IAggregationContainer.AutoDateHistogram { get; set; }
Expand Down Expand Up @@ -750,6 +758,11 @@ Func<CumulativeSumAggregationDescriptor, ICumulativeSumAggregation> selector
) =>
_SetInnerAggregation(name, selector, (a, d) => a.CumulativeSum = d);

public AggregationContainerDescriptor<T> CumulativeCardinality(string name,
Func<CumulativeCardinalityAggregationDescriptor, ICumulativeCardinalityAggregation> selector
) =>
_SetInnerAggregation(name, selector, (a, d) => a.CumulativeCardinality = d);

public AggregationContainerDescriptor<T> SerialDifferencing(string name,
Func<SerialDifferencingAggregationDescriptor, ISerialDifferencingAggregation> selector
) =>
Expand Down
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 { }
}
4 changes: 4 additions & 0 deletions src/Nest/Aggregations/Visitor/AggregationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public interface IAggregationVisitor

void Visit(ICumulativeSumAggregation aggregation);

void Visit(ICumulativeCardinalityAggregation aggregation);

void Visit(ISerialDifferencingAggregation aggregation);

void Visit(IBucketScriptAggregation aggregation);
Expand Down Expand Up @@ -179,6 +181,8 @@ public virtual void Visit(IPercentilesBucketAggregation aggregation) { }

public virtual void Visit(ICumulativeSumAggregation aggregation) { }

public virtual void Visit(ICumulativeCardinalityAggregation aggregation) { }

public virtual void Visit(IBucketScriptAggregation aggregation) { }

public virtual void Visit(ISamplerAggregation aggregation) { }
Expand Down
1 change: 1 addition & 0 deletions src/Nest/Aggregations/Visitor/AggregationWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void Walk(IAggregationContainer aggregation, IAggregationVisitor visitor)
Accept(v, d.Aggregations);
});
AcceptAggregation(aggregation.CumulativeSum, visitor, (v, d) => v.Visit(d));
AcceptAggregation(aggregation.CumulativeCardinality, visitor, (v, d) => v.Visit(d));
AcceptAggregation(aggregation.DateHistogram, visitor, (v, d) =>
{
v.Visit(d);
Expand Down
3 changes: 3 additions & 0 deletions src/Nest/XPack/Info/XPackInfo/XPackInfoResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class XPackFeatures
[DataMember(Name = "flattened")]
public XPackFeature Flattened { get; internal set; }

[DataMember(Name = "data_science")]
public XPackFeature DataScience { get; internal set; }

[DataMember(Name = "graph")]
public XPackFeature Graph { get; internal set; }

Expand Down
3 changes: 3 additions & 0 deletions src/Nest/XPack/Info/XPackUsage/XPackUsageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class XPackUsageResponse : ResponseBase
[DataMember(Name = "flattened")]
public XPackUsage Flattened { get; internal set; }

[DataMember(Name = "data_science")]
public XPackUsage DataScience { get; internal set; }

[DataMember(Name = "ilm")]
public IlmUsage IndexLifecycleManagement { get; internal set; }

Expand Down
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)

foreach (var item in projectsPerMonth.Buckets)
{
var commitsDerivative = item.Derivative("cumulative_commits");
var commitsDerivative = item.CumulativeSum("cumulative_commits");
commitsDerivative.Should().NotBeNull();
commitsDerivative.Value.Should().NotBe(null);
}
Expand Down

0 comments on commit 3c70247

Please sign in to comment.