-
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 support for Moving Function aggregation (#3419)
See elastic/elasticsearch#29594 (cherry picked from commit 6c4f69f)
- Loading branch information
Showing
5 changed files
with
152 additions
and
5 deletions.
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
41 changes: 41 additions & 0 deletions
41
src/Nest/Aggregations/Pipeline/MovingFunction/MovingFunctionAggregation.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,41 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Nest | ||
{ | ||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] | ||
[ContractJsonConverter(typeof(AggregationJsonConverter<MovingFunctionAggregation>))] | ||
public interface IMovingFunctionAggregation : IPipelineAggregation | ||
{ | ||
[JsonProperty("window")] | ||
int? Window { get; set; } | ||
|
||
[JsonProperty("script")] | ||
string Script { get; set; } | ||
} | ||
|
||
public class MovingFunctionAggregation | ||
: PipelineAggregationBase, IMovingFunctionAggregation | ||
{ | ||
internal MovingFunctionAggregation () { } | ||
|
||
public MovingFunctionAggregation(string name, SingleBucketsPath bucketsPath) | ||
: base(name, bucketsPath) { } | ||
|
||
internal override void WrapInContainer(AggregationContainer c) => c.MovingFunction = this; | ||
|
||
public int? Window { get; set; } | ||
public string Script { get; set; } | ||
} | ||
|
||
public class MovingFunctionAggregationDescriptor | ||
: PipelineAggregationDescriptorBase<MovingFunctionAggregationDescriptor, IMovingFunctionAggregation, SingleBucketsPath> | ||
, IMovingFunctionAggregation | ||
{ | ||
int? IMovingFunctionAggregation.Window { get; set; } | ||
string IMovingFunctionAggregation.Script { get; set; } | ||
|
||
public MovingFunctionAggregationDescriptor Window(int? windowSize) => Assign(a => a.Window = windowSize); | ||
|
||
public MovingFunctionAggregationDescriptor Script(string script) => Assign(a => a.Script = script); | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
...ts/Tests/Aggregations/Pipeline/MovingFunction/MovingAverageSimpleAggregationUsageTests.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,97 @@ | ||
using System; | ||
using System.Linq; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Core.Extensions; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Domain; | ||
using Tests.Framework; | ||
using Tests.Framework.Integration; | ||
|
||
namespace Tests.Aggregations.Pipeline.MovingFunction | ||
{ | ||
public class MovingFunctionAggregationUsageTests : AggregationUsageTestBase | ||
{ | ||
public MovingFunctionAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } | ||
|
||
protected override object AggregationJson => new | ||
{ | ||
projects_started_per_month = new | ||
{ | ||
date_histogram = new | ||
{ | ||
field = "startedOn", | ||
interval = "month", | ||
}, | ||
aggs = new | ||
{ | ||
commits = new | ||
{ | ||
sum = new | ||
{ | ||
field = "numberOfCommits" | ||
} | ||
}, | ||
commits_moving_avg = new | ||
{ | ||
moving_fn = new | ||
{ | ||
buckets_path = "commits", | ||
window = 30, | ||
script = "MovingFunctions.unweightedAvg(values)" | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a | ||
.DateHistogram("projects_started_per_month", dh => dh | ||
.Field(p => p.StartedOn) | ||
.Interval(DateInterval.Month) | ||
.Aggregations(aa => aa | ||
.Sum("commits", sm => sm | ||
.Field(p => p.NumberOfCommits) | ||
) | ||
.MovingFunction("commits_moving_avg", mv => mv | ||
.BucketsPath("commits") | ||
.Window(30) | ||
.Script("MovingFunctions.unweightedAvg(values)") | ||
) | ||
) | ||
); | ||
|
||
protected override AggregationDictionary InitializerAggs => | ||
new DateHistogramAggregation("projects_started_per_month") | ||
{ | ||
Field = "startedOn", | ||
Interval = DateInterval.Month, | ||
Aggregations = | ||
new SumAggregation("commits", "numberOfCommits") | ||
&& new MovingFunctionAggregation("commits_moving_avg", "commits") | ||
{ | ||
Window = 30, | ||
Script = "MovingFunctions.unweightedAvg(values)" | ||
} | ||
}; | ||
|
||
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); | ||
|
||
// average not calculated for the first bucket | ||
foreach(var item in projectsPerMonth.Buckets.Skip(1)) | ||
{ | ||
var movingAvg = item.Sum("commits_moving_avg"); | ||
movingAvg.Should().NotBeNull(); | ||
movingAvg.Value.Should().BeGreaterThan(0); | ||
} | ||
} | ||
} | ||
} |