-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add support for weighted average aggregation #3417
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
78 changes: 78 additions & 0 deletions
78
src/Nest/Aggregations/Metric/WeightedAverage/WeightedAverageAggregation.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,78 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using Newtonsoft.Json; | ||
|
||
namespace Nest | ||
{ | ||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] | ||
[ContractJsonConverter(typeof(AggregationJsonConverter<WeightedAverageAggregation>))] | ||
public interface IWeightedAverageAggregation : IAggregation | ||
{ | ||
/// <summary> The configuration for the field or script that provides the values </summary> | ||
[JsonProperty("value")] | ||
IWeightedAverageValue Value { get; set; } | ||
/// <summary> The configuration for the field or script that provides the weights /// </summary> | ||
[JsonProperty("weight")] | ||
IWeightedAverageValue Weight { get; set; } | ||
/// <summary> The optional numeric response formatter </summary> | ||
[JsonProperty("format")] | ||
string Format { get; set; } | ||
/// <summary> A hint about the values for pure scripts or unmapped fields </summary> | ||
[JsonProperty("value_type")] | ||
// TODO map as on server enum ? | ||
// https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/search/aggregations/support/ValueType.java | ||
string ValueType { get; set; } | ||
} | ||
|
||
public class WeightedAverageAggregation : AggregationBase, IWeightedAverageAggregation | ||
{ | ||
internal WeightedAverageAggregation() { } | ||
public WeightedAverageAggregation(string name) : base(name) { } | ||
|
||
internal override void WrapInContainer(AggregationContainer c) => c.WeightedAverage = this; | ||
|
||
/// <inheritdoc cref="IWeightedAverageAggregation.Value"/>> | ||
public IWeightedAverageValue Value { get; set; } | ||
/// <inheritdoc cref="IWeightedAverageAggregation.Weight"/>> | ||
public IWeightedAverageValue Weight { get; set; } | ||
/// <inheritdoc cref="IWeightedAverageAggregation.Format"/>> | ||
public string Format { get; set; } | ||
/// <inheritdoc cref="IWeightedAverageAggregation.ValueType"/>> | ||
public string ValueType { get; set; } | ||
} | ||
|
||
public class WeightedAverageAggregationDescriptor<T> | ||
: DescriptorBase<WeightedAverageAggregationDescriptor<T>, IWeightedAverageAggregation> | ||
, IWeightedAverageAggregation | ||
where T : class | ||
{ | ||
IWeightedAverageValue IWeightedAverageAggregation.Value { get; set; } | ||
IWeightedAverageValue IWeightedAverageAggregation.Weight { get; set; } | ||
string IWeightedAverageAggregation.Format { get; set; } | ||
string IWeightedAverageAggregation.ValueType { get; set; } | ||
|
||
string IAggregation.Name { get; set; } | ||
IDictionary<string, object> IAggregation.Meta { get; set; } | ||
/// <inheritdoc cref="IAggregation.Meta"/>> | ||
public WeightedAverageAggregationDescriptor<T> Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector) => | ||
Assign(a => a.Meta = selector?.Invoke(new FluentDictionary<string, object>())); | ||
|
||
/// <inheritdoc cref="IWeightedAverageAggregation.Value"/>> | ||
public WeightedAverageAggregationDescriptor<T> Value(Func<WeightedAverageValueDescriptor<T>, IWeightedAverageValue> selector) => | ||
Assign(a => a.Value = selector?.Invoke(new WeightedAverageValueDescriptor<T>())); | ||
|
||
/// <inheritdoc cref="IWeightedAverageAggregation.Weight"/>> | ||
public WeightedAverageAggregationDescriptor<T> Weight(Func<WeightedAverageValueDescriptor<T>, IWeightedAverageValue> selector) => | ||
Assign(a => a.Weight = selector?.Invoke(new WeightedAverageValueDescriptor<T>())); | ||
|
||
/// <inheritdoc cref="IWeightedAverageAggregation.Format"/>> | ||
public WeightedAverageAggregationDescriptor<T> Format(string format) => Assign(a => a.Format = format); | ||
|
||
/// <inheritdoc cref="IWeightedAverageAggregation.ValueType"/>> | ||
public WeightedAverageAggregationDescriptor<T> ValueType(string valueType) => Assign(a => a.ValueType = valueType); | ||
|
||
|
||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/Nest/Aggregations/Metric/WeightedAverage/WeightedAverageValue.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,52 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using Newtonsoft.Json; | ||
|
||
namespace Nest | ||
{ | ||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] | ||
[ContractJsonConverter(typeof(ReadAsTypeJsonConverter<WeightedAverageValue>))] | ||
public interface IWeightedAverageValue | ||
{ | ||
[JsonProperty("field")] | ||
Field Field { get; set; } | ||
|
||
[JsonProperty("script")] | ||
IScript Script { get; set; } | ||
|
||
[JsonProperty("missing")] | ||
double? Missing { get; set; } | ||
} | ||
|
||
public class WeightedAverageValue : IWeightedAverageValue | ||
{ | ||
internal WeightedAverageValue() { } | ||
public WeightedAverageValue(Field field) => this.Field = field; | ||
public WeightedAverageValue(IScript script) => this.Script = script; | ||
|
||
public Field Field { get; set; } | ||
public virtual IScript Script { get; set; } | ||
public double? Missing { get; set; } | ||
} | ||
|
||
public class WeightedAverageValueDescriptor<T> : DescriptorBase<WeightedAverageValueDescriptor<T>, IWeightedAverageValue> | ||
, IWeightedAverageValue | ||
where T : class | ||
{ | ||
Field IWeightedAverageValue.Field { get; set; } | ||
IScript IWeightedAverageValue.Script { get; set; } | ||
double? IWeightedAverageValue.Missing { get; set; } | ||
|
||
public WeightedAverageValueDescriptor<T> Field(Field field) => Assign(a => a.Field = field); | ||
|
||
public WeightedAverageValueDescriptor<T> Field(Expression<Func<T, object>> field) => Assign(a => a.Field = field); | ||
|
||
public virtual WeightedAverageValueDescriptor<T> Script(string script) => Assign(a => a.Script = (InlineScript)script); | ||
|
||
public virtual WeightedAverageValueDescriptor<T> Script(Func<ScriptDescriptor, IScript> scriptSelector) => | ||
Assign(a => a.Script = scriptSelector?.Invoke(new ScriptDescriptor())); | ||
|
||
public WeightedAverageValueDescriptor<T> Missing(double? missing) => Assign(a => a.Missing = missing); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/Tests/Tests/Aggregations/Metric/WeightedAverage/WeightedAverageAggregationUsageTests.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,61 @@ | ||
using System; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Framework.Integration; | ||
using static Nest.Infer; | ||
using Tests.Core.Extensions; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Domain; | ||
|
||
namespace Tests.Aggregations.Metric.WeightedAverage | ||
{ | ||
public class WeightedAverageAggregationUsageTests : AggregationUsageTestBase | ||
{ | ||
public WeightedAverageAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } | ||
|
||
private const string AggregationName = "weighted_avg_commits"; | ||
|
||
protected override object AggregationJson => new | ||
{ | ||
weighted_avg_commits = new | ||
{ | ||
weighted_avg = new | ||
{ | ||
value = new | ||
{ | ||
field = "numberOfCommits", | ||
missing = 0.0 | ||
}, | ||
weight = new | ||
{ | ||
field = "numberOfContributors" | ||
} | ||
} | ||
} | ||
}; | ||
|
||
protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a | ||
.WeightedAverage(AggregationName, avg => avg | ||
.Value(v => v.Field(p => p.NumberOfCommits).Missing(0)) | ||
.Weight(v => v.Field(p => p.NumberOfContributors)) | ||
); | ||
|
||
protected override AggregationDictionary InitializerAggs => | ||
new WeightedAverageAggregation(AggregationName) | ||
{ | ||
Value = new WeightedAverageValue(Field<Project>(p => p.NumberOfCommits)) | ||
{ | ||
Missing = 0 | ||
}, | ||
Weight = new WeightedAverageValue(Field<Project>(p => p.NumberOfContributors)) | ||
}; | ||
|
||
protected override void ExpectResponse(ISearchResponse<Project> response) | ||
{ | ||
response.ShouldBeValid(); | ||
var commitsAvg = response.Aggregations.WeightedAverage(AggregationName); | ||
commitsAvg.Should().NotBeNull(); | ||
commitsAvg.Value.Should().BeGreaterThan(0); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weighted Avg Aggregation is not marked as beta or experimental, so mapping as an enum might be better, as it would help guide users as to which values are supported.