12
12
import org .elasticsearch .common .io .stream .StreamInput ;
13
13
import org .elasticsearch .common .io .stream .StreamOutput ;
14
14
import org .elasticsearch .common .io .stream .Writeable ;
15
- import org .elasticsearch .common .xcontent .ObjectParser ;
16
- import org .elasticsearch .common .xcontent .ToXContentFragment ;
15
+ import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
16
+ import org .elasticsearch .common .xcontent .ToXContentObject ;
17
17
import org .elasticsearch .common .xcontent .XContentBuilder ;
18
+ import org .elasticsearch .common .xcontent .XContentParser ;
18
19
import org .elasticsearch .search .aggregations .metrics .avg .AvgAggregationBuilder ;
19
20
import org .elasticsearch .search .aggregations .metrics .max .MaxAggregationBuilder ;
20
21
import org .elasticsearch .search .aggregations .metrics .min .MinAggregationBuilder ;
32
33
import java .util .Objects ;
33
34
import java .util .stream .Collectors ;
34
35
36
+ import static org .elasticsearch .common .xcontent .ConstructingObjectParser .constructorArg ;
37
+
35
38
/**
36
39
* The configuration object for the metrics portion of a rollup job config
37
40
*
48
51
* ]
49
52
* }
50
53
*/
51
- public class MetricConfig implements Writeable , ToXContentFragment {
52
- private static final String NAME = "metric_config" ;
53
-
54
- private String field ;
55
- private List <String > metrics ;
56
-
57
- private static final ParseField FIELD = new ParseField ("field" );
58
- private static final ParseField METRICS = new ParseField ("metrics" );
54
+ public class MetricConfig implements Writeable , ToXContentObject {
59
55
60
56
// TODO: replace these with an enum
61
57
private static final ParseField MIN = new ParseField ("min" );
@@ -64,27 +60,54 @@ public class MetricConfig implements Writeable, ToXContentFragment {
64
60
private static final ParseField AVG = new ParseField ("avg" );
65
61
private static final ParseField VALUE_COUNT = new ParseField ("value_count" );
66
62
67
- public static final ObjectParser <MetricConfig .Builder , Void > PARSER = new ObjectParser <>(NAME , MetricConfig .Builder ::new );
68
-
63
+ private static final String NAME = "metrics" ;
64
+ private static final String FIELD = "field" ;
65
+ private static final String METRICS = "metrics" ;
66
+ private static final ConstructingObjectParser <MetricConfig , Void > PARSER ;
69
67
static {
70
- PARSER .declareString (MetricConfig .Builder ::setField , FIELD );
71
- PARSER .declareStringArray (MetricConfig .Builder ::setMetrics , METRICS );
68
+ PARSER = new ConstructingObjectParser <>(NAME , args -> {
69
+ @ SuppressWarnings ("unchecked" ) List <String > metrics = (List <String >) args [1 ];
70
+ return new MetricConfig ((String ) args [0 ], metrics );
71
+ });
72
+ PARSER .declareString (constructorArg (), new ParseField (FIELD ));
73
+ PARSER .declareStringArray (constructorArg (), new ParseField (METRICS ));
72
74
}
73
75
74
- MetricConfig (String name , List <String > metrics ) {
75
- this .field = name ;
76
+ private final String field ;
77
+ private final List <String > metrics ;
78
+
79
+ public MetricConfig (final String field , final List <String > metrics ) {
80
+ if (field == null || field .isEmpty ()) {
81
+ throw new IllegalArgumentException ("Field must be a non-null, non-empty string" );
82
+ }
83
+ if (metrics == null || metrics .isEmpty ()) {
84
+ throw new IllegalArgumentException ("Metrics must be a non-null, non-empty array of strings" );
85
+ }
86
+ metrics .forEach (m -> {
87
+ if (RollupField .SUPPORTED_METRICS .contains (m ) == false ) {
88
+ throw new IllegalArgumentException ("Unsupported metric [" + m + "]. " +
89
+ "Supported metrics include: " + RollupField .SUPPORTED_METRICS );
90
+ }
91
+ });
92
+ this .field = field ;
76
93
this .metrics = metrics ;
77
94
}
78
95
79
- MetricConfig (StreamInput in ) throws IOException {
96
+ MetricConfig (final StreamInput in ) throws IOException {
80
97
field = in .readString ();
81
98
metrics = in .readList (StreamInput ::readString );
82
99
}
83
100
101
+ /**
102
+ * @return the name of the field used in the metric configuration. Never {@code null}.
103
+ */
84
104
public String getField () {
85
105
return field ;
86
106
}
87
107
108
+ /**
109
+ * @return the names of the metrics used in the metric configuration. Never {@code null}.
110
+ */
88
111
public List <String > getMetrics () {
89
112
return metrics ;
90
113
}
@@ -159,10 +182,13 @@ public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa
159
182
}
160
183
161
184
@ Override
162
- public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
163
- builder .field (FIELD .getPreferredName (), field );
164
- builder .field (METRICS .getPreferredName (), metrics );
165
- return builder ;
185
+ public XContentBuilder toXContent (final XContentBuilder builder , final Params params ) throws IOException {
186
+ builder .startObject ();
187
+ {
188
+ builder .field (FIELD , field );
189
+ builder .field (METRICS , metrics );
190
+ }
191
+ return builder .endObject ();
166
192
}
167
193
168
194
@ Override
@@ -172,19 +198,16 @@ public void writeTo(StreamOutput out) throws IOException {
172
198
}
173
199
174
200
@ Override
175
- public boolean equals (Object other ) {
201
+ public boolean equals (final Object other ) {
176
202
if (this == other ) {
177
203
return true ;
178
204
}
179
-
180
205
if (other == null || getClass () != other .getClass ()) {
181
206
return false ;
182
207
}
183
208
184
- MetricConfig that = (MetricConfig ) other ;
185
-
186
- return Objects .equals (this .field , that .field )
187
- && Objects .equals (this .metrics , that .metrics );
209
+ final MetricConfig that = (MetricConfig ) other ;
210
+ return Objects .equals (field , that .field ) && Objects .equals (metrics , that .metrics );
188
211
}
189
212
190
213
@ Override
@@ -197,52 +220,7 @@ public String toString() {
197
220
return Strings .toString (this , true , true );
198
221
}
199
222
200
-
201
- public static class Builder {
202
- private String field ;
203
- private List <String > metrics ;
204
-
205
- public Builder () {
206
- }
207
-
208
- public Builder (MetricConfig config ) {
209
- this .field = config .getField ();
210
- this .metrics = config .getMetrics ();
211
- }
212
-
213
- public String getField () {
214
- return field ;
215
- }
216
-
217
- public MetricConfig .Builder setField (String field ) {
218
- this .field = field ;
219
- return this ;
220
- }
221
-
222
- public List <String > getMetrics () {
223
- return metrics ;
224
- }
225
-
226
- public MetricConfig .Builder setMetrics (List <String > metrics ) {
227
- this .metrics = metrics ;
228
- return this ;
229
- }
230
-
231
- public MetricConfig build () {
232
- if (Strings .isNullOrEmpty (field ) == true ) {
233
- throw new IllegalArgumentException ("Parameter [" + FIELD .getPreferredName () + "] must be a non-null, non-empty string." );
234
- }
235
- if (metrics == null || metrics .isEmpty ()) {
236
- throw new IllegalArgumentException ("Parameter [" + METRICS .getPreferredName ()
237
- + "] must be a non-null, non-empty array of strings." );
238
- }
239
- metrics .forEach (m -> {
240
- if (RollupField .SUPPORTED_METRICS .contains (m ) == false ) {
241
- throw new IllegalArgumentException ("Unsupported metric [" + m + "]. " +
242
- "Supported metrics include: " + RollupField .SUPPORTED_METRICS );
243
- }
244
- });
245
- return new MetricConfig (field , metrics );
246
- }
223
+ public static MetricConfig fromXContent (final XContentParser parser ) throws IOException {
224
+ return PARSER .parse (parser , null );
247
225
}
248
226
}
0 commit comments