Skip to content

Commit e291bc7

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Add support for rate aggregation (#5206)
* Add support for rate aggregation * Add missing XML comments
1 parent e60aeee commit e291bc7

File tree

8 files changed

+352
-0
lines changed

8 files changed

+352
-0
lines changed

docs/aggregations.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ The values are typically extracted from the fields of the document (using the fi
5656

5757
* <<percentiles-aggregation-usage,Percentiles Aggregation Usage>>
5858

59+
* <<rate-aggregation-usage,Rate Aggregation Usage>>
60+
5961
* <<scripted-metric-aggregation-usage,Scripted Metric Aggregation Usage>>
6062

6163
* <<stats-aggregation-usage,Stats Aggregation Usage>>
@@ -100,6 +102,8 @@ include::aggregations/metric/percentile-ranks/percentile-ranks-aggregation-usage
100102

101103
include::aggregations/metric/percentiles/percentiles-aggregation-usage.asciidoc[]
102104

105+
include::aggregations/metric/rate/rate-aggregation-usage.asciidoc[]
106+
103107
include::aggregations/metric/scripted-metric/scripted-metric-aggregation-usage.asciidoc[]
104108

105109
include::aggregations/metric/stats/stats-aggregation-usage.asciidoc[]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/7.11
2+
3+
:github: https://github.com/elastic/elasticsearch-net
4+
5+
:nuget: https://www.nuget.org/packages
6+
7+
////
8+
IMPORTANT NOTE
9+
==============
10+
This file has been generated from https://github.com/elastic/elasticsearch-net/tree/7.x/src/Tests/Tests/Aggregations/Metric/Rate/RateAggregationUsageTests.cs.
11+
If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,
12+
please modify the original csharp file found at the link and submit the PR with that change. Thanks!
13+
////
14+
15+
[[rate-aggregation-usage]]
16+
=== Rate Aggregation Usage
17+
18+
A rate metrics aggregation can be used only inside a date_histogram and calculates a rate of documents or a field in each
19+
date_histogram bucket. The field values can be generated by a provided script or extracted from specific numeric or histogram fields in the documents.
20+
21+
Be sure to read the Elasticsearch documentation on {ref_current}/search-aggregations-metrics-rate-aggregation.html[Rate Aggregation].
22+
23+
==== Fluent DSL example
24+
25+
[source,csharp]
26+
----
27+
a => a
28+
.DateHistogram("by_date", d => d
29+
.Field(f => f.StartedOn)
30+
.CalendarInterval(DateInterval.Month)
31+
.Aggregations(a => a
32+
.Rate("my_rate", m => m
33+
.Field(p => p.NumberOfCommits)
34+
.Unit(DateInterval.Month)
35+
.Mode(RateMode.Sum)
36+
)))
37+
----
38+
39+
==== Object Initializer syntax example
40+
41+
[source,csharp]
42+
----
43+
new DateHistogramAggregation("by_date")
44+
{
45+
Field = Field<Project>(p => p.StartedOn),
46+
CalendarInterval = DateInterval.Month,
47+
Aggregations = new RateAggregation("my_rate", Field<Project>(p => p.NumberOfCommits))
48+
{
49+
Unit = DateInterval.Month,
50+
Mode = RateMode.Sum
51+
}
52+
}
53+
----
54+
55+
[source,javascript]
56+
.Example json output
57+
----
58+
{
59+
"by_date": {
60+
"date_histogram": {
61+
"field": "startedOn",
62+
"calendar_interval": "month"
63+
},
64+
"aggs": {
65+
"my_rate": {
66+
"rate": {
67+
"field": "numberOfCommits",
68+
"unit": "month",
69+
"mode": "sum"
70+
}
71+
}
72+
}
73+
}
74+
}
75+
----
76+
77+
==== Handling Responses
78+
79+
[source,csharp]
80+
----
81+
response.ShouldBeValid();
82+
83+
var dateHistogram = response.Aggregations.DateHistogram("by_date");
84+
dateHistogram.Should().NotBeNull();
85+
dateHistogram.Buckets.Should().NotBeNull();
86+
dateHistogram.Buckets.Count.Should().BeGreaterThan(10);
87+
foreach (var item in dateHistogram.Buckets)
88+
{
89+
var rate = item.Rate("my_rate");
90+
rate.Should().NotBeNull();
91+
rate.Value.Should().BeGreaterOrEqualTo(1);
92+
}
93+
----
94+

src/Nest/Aggregations/AggregateDictionary.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public MultiBucketAggregate<RareTermsBucket<TKey>> RareTerms<TKey>(string key)
206206
};
207207
}
208208

209+
public ValueAggregate Rate(string key) => TryGet<ValueAggregate>(key);
210+
209211
public MultiBucketAggregate<RareTermsBucket<string>> RareTerms(string key) => RareTerms<string>(key);
210212

211213
public MultiBucketAggregate<RangeBucket> Range(string key) => GetMultiBucketAggregate<RangeBucket>(key);

src/Nest/Aggregations/AggregationContainer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ public interface IAggregationContainer
232232
[DataMember(Name = "rare_terms")]
233233
IRareTermsAggregation RareTerms { get; set; }
234234

235+
[DataMember(Name = "rate")]
236+
IRateAggregation Rate { get; set; }
237+
235238
[DataMember(Name = "reverse_nested")]
236239
IReverseNestedAggregation ReverseNested { get; set; }
237240

@@ -396,6 +399,8 @@ public class AggregationContainer : IAggregationContainer
396399

397400
public IRareTermsAggregation RareTerms { get; set; }
398401

402+
public IRateAggregation Rate { get; set; }
403+
399404
public IReverseNestedAggregation ReverseNested { get; set; }
400405

401406
public ISamplerAggregation Sampler { get; set; }
@@ -558,6 +563,8 @@ public class AggregationContainerDescriptor<T> : DescriptorBase<AggregationConta
558563

559564
IRareTermsAggregation IAggregationContainer.RareTerms { get; set; }
560565

566+
IRateAggregation IAggregationContainer.Rate { get; set; }
567+
561568
IReverseNestedAggregation IAggregationContainer.ReverseNested { get; set; }
562569

563570
ISamplerAggregation IAggregationContainer.Sampler { get; set; }
@@ -732,6 +739,10 @@ Func<RareTermsAggregationDescriptor<T>, IRareTermsAggregation> selector
732739
) =>
733740
_SetInnerAggregation(name, selector, (a, d) => a.RareTerms = d);
734741

742+
public AggregationContainerDescriptor<T> Rate(string name,
743+
Func<RateAggregationDescriptor<T>, IRateAggregation> selector) =>
744+
_SetInnerAggregation(name, selector, (a, d) => a.Rate = d);
745+
735746
public AggregationContainerDescriptor<T> Stats(string name,
736747
Func<StatsAggregationDescriptor<T>, IStatsAggregation> selector
737748
) =>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Runtime.Serialization;
6+
using Elasticsearch.Net;
7+
using Elasticsearch.Net.Utf8Json;
8+
9+
namespace Nest
10+
{
11+
[InterfaceDataContract]
12+
[ReadAs(typeof(RateAggregation))]
13+
public interface IRateAggregation : IMetricAggregation
14+
{
15+
/// <summary>
16+
/// The <see cref="DateInterval"/> to use as the rate unit.
17+
/// </summary>
18+
[DataMember(Name = "unit")]
19+
DateInterval? Unit { get; set; }
20+
21+
/// <summary>
22+
/// The mode to use in the rate calculation. By default, "sum" mode is used.
23+
/// </summary>
24+
/// <remarks>
25+
/// The mode may be either "sum", where the rate calculates the sum of field values, or
26+
/// "value_count", where the rate uses the number of values in the field.
27+
/// </remarks>
28+
[DataMember(Name = "mode")]
29+
RateMode? Mode { get; set; }
30+
}
31+
32+
public class RateAggregation : MetricAggregationBase, IRateAggregation
33+
{
34+
public RateAggregation(string name) : base(name, null) { }
35+
public RateAggregation(string name, Field field) : base(name, field) { }
36+
37+
internal override void WrapInContainer(AggregationContainer c) => c.Rate = this;
38+
39+
/// <inheritdoc cref ="IRateAggregation.Unit"/>
40+
public DateInterval? Unit { get; set; }
41+
42+
/// <inheritdoc cref ="IRateAggregation.Mode"/>
43+
public RateMode? Mode { get; set; }
44+
}
45+
46+
public class RateAggregationDescriptor<T>
47+
: MetricAggregationDescriptorBase<RateAggregationDescriptor<T>, IRateAggregation, T>, IRateAggregation
48+
where T : class
49+
{
50+
DateInterval? IRateAggregation.Unit { get; set; }
51+
52+
RateMode? IRateAggregation.Mode { get; set; }
53+
54+
/// <inheritdoc cref ="IRateAggregation.Unit"/>
55+
public RateAggregationDescriptor<T> Unit(DateInterval? dateInterval) =>
56+
Assign(dateInterval, (a, v) => a.Unit = v);
57+
58+
/// <inheritdoc cref ="IRateAggregation.Mode"/>
59+
public RateAggregationDescriptor<T> Mode(RateMode? mode) =>
60+
Assign(mode, (a, v) => a.Mode = v);
61+
}
62+
63+
[StringEnum]
64+
public enum RateMode
65+
{
66+
/// <summary>
67+
/// Rate calculates the sum of field values.
68+
/// </summary>
69+
[EnumMember(Value = "sum")]
70+
Sum,
71+
72+
/// <summary>
73+
/// Rate uses the number of values in the field.
74+
/// </summary>
75+
[EnumMember(Value = "value_count")]
76+
ValueCount
77+
}
78+
}

src/Nest/Aggregations/Visitor/AggregationVisitor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public interface IAggregationVisitor
8282

8383
void Visit(IRareTermsAggregation aggregation);
8484

85+
void Visit(IRateAggregation aggregation);
86+
8587
void Visit(ITermsAggregation aggregation);
8688

8789
void Visit(ISignificantTermsAggregation aggregation);
@@ -241,6 +243,8 @@ public virtual void Visit(IRangeAggregation aggregation) { }
241243

242244
public virtual void Visit(IRareTermsAggregation aggregation) { }
243245

246+
public virtual void Visit(IRateAggregation aggregation) { }
247+
244248
public virtual void Visit(INestedAggregation aggregation) { }
245249

246250
public virtual void Visit(INormalizeAggregation aggregation) { }

src/Nest/Aggregations/Visitor/AggregationWalker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public void Walk(IAggregationContainer aggregation, IAggregationVisitor visitor)
144144
v.Visit(d);
145145
Accept(v, d.Aggregations);
146146
});
147+
AcceptAggregation(aggregation.Rate, visitor, (v, d) => v.Visit(d));
147148
AcceptAggregation(aggregation.ReverseNested, visitor, (v, d) =>
148149
{
149150
v.Visit(d);

0 commit comments

Comments
 (0)