5
5
*/
6
6
package org .elasticsearch .xpack .analytics .rate ;
7
7
8
+ import java .io .IOException ;
9
+ import java .util .Map ;
10
+ import java .util .Objects ;
11
+
12
+ import org .elasticsearch .Version ;
8
13
import org .elasticsearch .common .ParseField ;
9
14
import org .elasticsearch .common .Rounding ;
10
15
import org .elasticsearch .common .io .stream .StreamInput ;
24
29
import org .elasticsearch .search .aggregations .support .ValuesSourceRegistry ;
25
30
import org .elasticsearch .search .aggregations .support .ValuesSourceType ;
26
31
27
- import java .io .IOException ;
28
- import java .util .Map ;
29
- import java .util .Objects ;
30
-
31
32
public class RateAggregationBuilder extends ValuesSourceAggregationBuilder .LeafOnly <ValuesSource , RateAggregationBuilder > {
32
33
public static final String NAME = "rate" ;
33
34
public static final ParseField UNIT_FIELD = new ParseField ("unit" );
35
+ public static final ParseField MODE_FIELD = new ParseField ("mode" );
34
36
public static final ValuesSourceRegistry .RegistryKey <RateAggregatorSupplier > REGISTRY_KEY = new ValuesSourceRegistry .RegistryKey <>(
35
37
NAME ,
36
38
RateAggregatorSupplier .class
@@ -40,9 +42,11 @@ public class RateAggregationBuilder extends ValuesSourceAggregationBuilder.LeafO
40
42
static {
41
43
ValuesSourceAggregationBuilder .declareFields (PARSER , true , true , false , false );
42
44
PARSER .declareString (RateAggregationBuilder ::rateUnit , UNIT_FIELD );
45
+ PARSER .declareString (RateAggregationBuilder ::rateMode , MODE_FIELD );
43
46
}
44
47
45
48
Rounding .DateTimeUnit rateUnit ;
49
+ RateMode rateMode ;
46
50
47
51
public static void registerAggregators (ValuesSourceRegistry .Builder builder ) {
48
52
RateAggregatorFactory .registerAggregators (builder );
@@ -58,6 +62,8 @@ protected RateAggregationBuilder(
58
62
Map <String , Object > metadata
59
63
) {
60
64
super (clone , factoriesBuilder , metadata );
65
+ this .rateUnit = clone .rateUnit ;
66
+ this .rateMode = clone .rateMode ;
61
67
}
62
68
63
69
@ Override
@@ -76,6 +82,11 @@ public RateAggregationBuilder(StreamInput in) throws IOException {
76
82
} else {
77
83
rateUnit = null ;
78
84
}
85
+ if (in .getVersion ().onOrAfter (Version .V_7_11_0 )) {
86
+ if (in .readBoolean ()) {
87
+ rateMode = in .readEnum (RateMode .class );
88
+ }
89
+ }
79
90
}
80
91
81
92
@ Override
@@ -90,6 +101,14 @@ protected void innerWriteTo(StreamOutput out) throws IOException {
90
101
} else {
91
102
out .writeByte ((byte ) 0 );
92
103
}
104
+ if (out .getVersion ().onOrAfter (Version .V_7_11_0 )) {
105
+ if (rateMode != null ) {
106
+ out .writeBoolean (true );
107
+ out .writeEnum (rateMode );
108
+ } else {
109
+ out .writeBoolean (false );
110
+ }
111
+ }
93
112
}
94
113
95
114
@ Override
@@ -104,14 +123,22 @@ protected RateAggregatorFactory innerBuild(
104
123
AggregatorFactory parent ,
105
124
AggregatorFactories .Builder subFactoriesBuilder
106
125
) throws IOException {
107
- return new RateAggregatorFactory (name , config , rateUnit , context , parent , subFactoriesBuilder , metadata );
126
+ if (field () == null && script () == null ) {
127
+ if (rateMode != null ) {
128
+ throw new IllegalArgumentException ("The mode parameter is only supported with field or script" );
129
+ }
130
+ }
131
+ return new RateAggregatorFactory (name , config , rateUnit , rateMode , context , parent , subFactoriesBuilder , metadata );
108
132
}
109
133
110
134
@ Override
111
135
public XContentBuilder doXContentBody (XContentBuilder builder , Params params ) throws IOException {
112
136
if (rateUnit != null ) {
113
137
builder .field (UNIT_FIELD .getPreferredName (), rateUnit .shortName ());
114
138
}
139
+ if (rateMode != null ) {
140
+ builder .field (MODE_FIELD .getPreferredName (), rateMode .value ());
141
+ }
115
142
return builder ;
116
143
}
117
144
@@ -129,6 +156,15 @@ public RateAggregationBuilder rateUnit(Rounding.DateTimeUnit rateUnit) {
129
156
return this ;
130
157
}
131
158
159
+ public RateAggregationBuilder rateMode (String rateMode ) {
160
+ return rateMode (RateMode .resolve (rateMode ));
161
+ }
162
+
163
+ public RateAggregationBuilder rateMode (RateMode rateMode ) {
164
+ this .rateMode = rateMode ;
165
+ return this ;
166
+ }
167
+
132
168
static Rounding .DateTimeUnit parse (String rateUnit ) {
133
169
Rounding .DateTimeUnit parsedRate = DateHistogramAggregationBuilder .DATE_FIELD_UNITS .get (rateUnit );
134
170
if (parsedRate == null ) {
@@ -140,17 +176,7 @@ static Rounding.DateTimeUnit parse(String rateUnit) {
140
176
@ Override
141
177
protected ValuesSourceConfig resolveConfig (AggregationContext context ) {
142
178
if (field () == null && script () == null ) {
143
- return new ValuesSourceConfig (
144
- CoreValuesSourceType .NUMERIC ,
145
- null ,
146
- true ,
147
- null ,
148
- null ,
149
- 1.0 ,
150
- null ,
151
- DocValueFormat .RAW ,
152
- context
153
- );
179
+ return new ValuesSourceConfig (CoreValuesSourceType .NUMERIC , null , true , null , null , 1.0 , null , DocValueFormat .RAW , context );
154
180
} else {
155
181
return super .resolveConfig (context );
156
182
}
@@ -162,11 +188,11 @@ public boolean equals(Object o) {
162
188
if (o == null || getClass () != o .getClass ()) return false ;
163
189
if (!super .equals (o )) return false ;
164
190
RateAggregationBuilder that = (RateAggregationBuilder ) o ;
165
- return rateUnit == that .rateUnit ;
191
+ return rateUnit == that .rateUnit && rateMode == that . rateMode ;
166
192
}
167
193
168
194
@ Override
169
195
public int hashCode () {
170
- return Objects .hash (super .hashCode (), rateUnit );
196
+ return Objects .hash (super .hashCode (), rateUnit , rateMode );
171
197
}
172
198
}
0 commit comments