-
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.
Browse files
Browse the repository at this point in the history
Relates: elastic/elasticsearch#56399 This commit adds the normalize aggregation to the high level client. Co-authored-by: Russ Cam <[email protected]>
- Loading branch information
1 parent
035471b
commit 19b76e2
Showing
10 changed files
with
332 additions
and
0 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
106 changes: 106 additions & 0 deletions
106
docs/aggregations/pipeline/normalize/normalize-aggregation-usage.asciidoc
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,106 @@ | ||
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/master | ||
|
||
:github: https://github.com/elastic/elasticsearch-net | ||
|
||
:nuget: https://www.nuget.org/packages | ||
|
||
//// | ||
IMPORTANT NOTE | ||
============== | ||
This file has been generated from https://github.com/elastic/elasticsearch-net/tree/master/src/Tests/Tests/Aggregations/Pipeline/Normalize/NormalizeAggregationUsageTests.cs. | ||
If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file, | ||
please modify the original csharp file found at the link and submit the PR with that change. Thanks! | ||
//// | ||
|
||
[[normalize-aggregation-usage]] | ||
=== Normalize Aggregation Usage | ||
|
||
A parent pipeline aggregation which calculates the specific normalized/rescaled value for a specific bucket value. | ||
Values that cannot be normalized, will be skipped using the skip gap policy. | ||
|
||
NOTE: Valid for Elasticsearch 7.9.0+ with at least basic license level | ||
|
||
==== Fluent DSL example | ||
|
||
[source,csharp] | ||
---- | ||
a => a | ||
.DateHistogram("projects_started_per_month", dh => dh | ||
.Field(p => p.StartedOn) | ||
.CalendarInterval(DateInterval.Month) | ||
.Aggregations(aa => aa | ||
.Sum("commits", sm => sm | ||
.Field(p => p.NumberOfCommits) | ||
) | ||
.Normalize("percent_of_commits", aaa => aaa | ||
.BucketsPath("commits") | ||
.Method(NormalizeMethod.PercentOfSum) | ||
.Format("00.00%") | ||
) | ||
) | ||
) | ||
---- | ||
|
||
==== Object Initializer syntax example | ||
|
||
[source,csharp] | ||
---- | ||
new DateHistogramAggregation("projects_started_per_month") | ||
{ | ||
Field = "startedOn", | ||
CalendarInterval = DateInterval.Month, | ||
Aggregations = new SumAggregation("commits", "numberOfCommits") && | ||
new NormalizeAggregation("percent_of_commits", "commits") | ||
{ | ||
Method = NormalizeMethod.PercentOfSum, | ||
Format = "00.00%" | ||
} | ||
} | ||
---- | ||
|
||
[source,javascript] | ||
.Example json output | ||
---- | ||
{ | ||
"projects_started_per_month": { | ||
"date_histogram": { | ||
"field": "startedOn", | ||
"calendar_interval": "month" | ||
}, | ||
"aggs": { | ||
"commits": { | ||
"sum": { | ||
"field": "numberOfCommits" | ||
} | ||
}, | ||
"percent_of_commits": { | ||
"normalize": { | ||
"buckets_path": "commits", | ||
"method": "percent_of_sum", | ||
"format": "00.00%" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
---- | ||
|
||
==== Handling Responses | ||
|
||
[source,csharp] | ||
---- | ||
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 bucket in projectsPerMonth.Buckets) | ||
{ | ||
var normalize = bucket.Normalize("percent_of_commits"); | ||
normalize.Value.Should().BeGreaterOrEqualTo(0); | ||
normalize.ValueAsString.Should().NotBeNullOrEmpty(); | ||
} | ||
---- | ||
|
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
91 changes: 91 additions & 0 deletions
91
src/Nest/Aggregations/Pipeline/Normalize/NormalizeAggregation.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,91 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System.Runtime.Serialization; | ||
using Elasticsearch.Net; | ||
using Elasticsearch.Net.Utf8Json; | ||
|
||
namespace Nest | ||
{ | ||
/// <summary> | ||
/// A parent pipeline aggregation which calculates the specific normalized/rescaled value for a specific bucket value. | ||
/// Values that cannot be normalized, will be skipped using the skip gap policy. | ||
/// <para /> | ||
/// Valid in Elasticsearch 7.9.0+ with at least basic license level. | ||
/// </summary> | ||
[InterfaceDataContract] | ||
[ReadAs(typeof(NormalizeAggregation))] | ||
public interface INormalizeAggregation : IPipelineAggregation | ||
{ | ||
[DataMember(Name = "method")] | ||
NormalizeMethod Method { get; set; } | ||
} | ||
|
||
/// <inheritdoc cref="INormalizeAggregation"/> | ||
public class NormalizeAggregation | ||
: PipelineAggregationBase, INormalizeAggregation | ||
{ | ||
internal NormalizeAggregation() { } | ||
|
||
public NormalizeAggregation(string name, SingleBucketsPath bucketsPath) | ||
: base(name, bucketsPath) { } | ||
|
||
internal override void WrapInContainer(AggregationContainer c) => c.Normalize = this; | ||
|
||
/// <inheritdoc cref ="INormalizeAggregation.Method"/> | ||
public NormalizeMethod Method { get; set; } | ||
} | ||
|
||
/// <inheritdoc cref="INormalizeAggregation"/> | ||
public class NormalizeAggregationDescriptor | ||
: PipelineAggregationDescriptorBase<NormalizeAggregationDescriptor, INormalizeAggregation, SingleBucketsPath> | ||
, INormalizeAggregation | ||
{ | ||
NormalizeMethod INormalizeAggregation.Method { get; set; } | ||
|
||
/// <inheritdoc cref ="INormalizeAggregation.Method"/> | ||
public NormalizeAggregationDescriptor Method(NormalizeMethod method) => | ||
Assign(method, (a, v) => a.Method = v); | ||
} | ||
|
||
[StringEnum] | ||
public enum NormalizeMethod | ||
{ | ||
/// <summary> | ||
/// rescales the data such that the minimum number is zero, and the maximum number is 1, with the rest normalized linearly in-between. | ||
/// </summary> | ||
[EnumMember(Value = "rescale_0_1")] | ||
RescaleZeroToOne, | ||
|
||
/// <summary> | ||
/// rescales the data such that the minimum number is zero, and the maximum number is 1, with the rest normalized linearly in-between. | ||
/// </summary> | ||
[EnumMember(Value = "rescale_0_100")] | ||
RescaleZeroToOneHundred, | ||
|
||
/// <summary> | ||
/// normalizes each value so that it represents a percentage of the total sum it attributes to. | ||
/// </summary> | ||
[EnumMember(Value = "percent_of_sum")] | ||
PercentOfSum, | ||
|
||
/// <summary> | ||
/// normalizes such that each value is normalized by how much it differs from the average. | ||
/// </summary> | ||
[EnumMember(Value = "mean")] | ||
Mean, | ||
|
||
/// <summary> | ||
/// normalizes such that each value represents how far it is from the mean relative to the standard deviation | ||
/// </summary> | ||
[EnumMember(Value = "zscore")] | ||
Zscore, | ||
|
||
/// <summary> | ||
/// normalizes such that each value is exponentiated and relative to the sum of the exponents of the original values. | ||
/// </summary> | ||
[EnumMember(Value = "softmax")] | ||
Softmax | ||
} | ||
} |
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
Oops, something went wrong.