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 .bucket .composite .CompositeValuesSourceBuilder ;
19
20
import org .elasticsearch .search .aggregations .bucket .composite .HistogramValuesSourceBuilder ;
20
21
import org .elasticsearch .search .aggregations .bucket .histogram .HistogramAggregationBuilder ;
27
28
import java .util .List ;
28
29
import java .util .Map ;
29
30
import java .util .Objects ;
30
- import java .util .Set ;
31
31
import java .util .stream .Collectors ;
32
32
33
+ import static org .elasticsearch .common .xcontent .ConstructingObjectParser .constructorArg ;
34
+
33
35
/**
34
36
* The configuration object for the histograms in the rollup config
35
37
*
42
44
* ]
43
45
* }
44
46
*/
45
- public class HistoGroupConfig implements Writeable , ToXContentFragment {
46
- private static final String NAME = "histo_group_config" ;
47
- public static final ObjectParser <HistoGroupConfig .Builder , Void > PARSER
48
- = new ObjectParser <>(NAME , HistoGroupConfig .Builder ::new );
47
+ public class HistogramGroupConfig implements Writeable , ToXContentObject {
49
48
50
- private static final ParseField INTERVAL = new ParseField ("interval" );
51
- private static final ParseField FIELDS = new ParseField ("fields" );
49
+ public static final String NAME = "histogram" ;
50
+ private static final String INTERVAL = "interval" ;
51
+ private static final String FIELDS = "fields" ;
52
+ private static final ConstructingObjectParser <HistogramGroupConfig , Void > PARSER ;
53
+ static {
54
+ PARSER = new ConstructingObjectParser <>(NAME , args -> {
55
+ @ SuppressWarnings ("unchecked" ) List <String > fields = (List <String >) args [1 ];
56
+ return new HistogramGroupConfig ((long ) args [0 ], fields != null ? fields .toArray (new String [fields .size ()]) : null );
57
+ });
58
+ PARSER .declareLong (constructorArg (), new ParseField (INTERVAL ));
59
+ PARSER .declareStringArray (constructorArg (), new ParseField (FIELDS ));
60
+ }
52
61
53
62
private final long interval ;
54
63
private final String [] fields ;
55
64
56
- static {
57
- PARSER .declareLong (HistoGroupConfig .Builder ::setInterval , INTERVAL );
58
- PARSER .declareStringArray (HistoGroupConfig .Builder ::setFields , FIELDS );
59
- }
60
-
61
- private HistoGroupConfig (long interval , String [] fields ) {
65
+ public HistogramGroupConfig (final long interval , final String ... fields ) {
66
+ if (interval <= 0 ) {
67
+ throw new IllegalArgumentException ("Interval must be a positive long" );
68
+ }
69
+ if (fields == null || fields .length == 0 ) {
70
+ throw new IllegalArgumentException ("Fields must have at least one value" );
71
+ }
62
72
this .interval = interval ;
63
73
this .fields = fields ;
64
74
}
65
75
66
- HistoGroupConfig ( StreamInput in ) throws IOException {
76
+ HistogramGroupConfig ( final StreamInput in ) throws IOException {
67
77
interval = in .readVLong ();
68
78
fields = in .readStringArray ();
69
79
}
@@ -101,18 +111,14 @@ public List<CompositeValuesSourceBuilder<?>> toBuilders() {
101
111
public Map <String , Object > toAggCap () {
102
112
Map <String , Object > map = new HashMap <>(2 );
103
113
map .put ("agg" , HistogramAggregationBuilder .NAME );
104
- map .put (INTERVAL . getPreferredName () , interval );
114
+ map .put (INTERVAL , interval );
105
115
return map ;
106
116
}
107
117
108
118
public Map <String , Object > getMetadata () {
109
119
return Collections .singletonMap (RollupField .formatMetaField (RollupField .INTERVAL ), interval );
110
120
}
111
121
112
- public Set <String > getAllFields () {
113
- return Arrays .stream (fields ).collect (Collectors .toSet ());
114
- }
115
-
116
122
public void validateMappings (Map <String , Map <String , FieldCapabilities >> fieldCapsResponse ,
117
123
ActionRequestValidationException validationException ) {
118
124
@@ -138,9 +144,13 @@ public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa
138
144
}
139
145
140
146
@ Override
141
- public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
142
- builder .field (INTERVAL .getPreferredName (), interval );
143
- builder .field (FIELDS .getPreferredName (), fields );
147
+ public XContentBuilder toXContent (final XContentBuilder builder , final Params params ) throws IOException {
148
+ builder .startObject ();
149
+ {
150
+ builder .field (INTERVAL , interval );
151
+ builder .field (FIELDS , fields );
152
+ }
153
+ builder .endObject ();
144
154
return builder ;
145
155
}
146
156
@@ -151,19 +161,15 @@ public void writeTo(StreamOutput out) throws IOException {
151
161
}
152
162
153
163
@ Override
154
- public boolean equals (Object other ) {
164
+ public boolean equals (final Object other ) {
155
165
if (this == other ) {
156
166
return true ;
157
167
}
158
-
159
168
if (other == null || getClass () != other .getClass ()) {
160
169
return false ;
161
170
}
162
-
163
- HistoGroupConfig that = (HistoGroupConfig ) other ;
164
-
165
- return Objects .equals (this .interval , that .interval )
166
- && Arrays .equals (this .fields , that .fields );
171
+ final HistogramGroupConfig that = (HistogramGroupConfig ) other ;
172
+ return Objects .equals (interval , that .interval ) && Arrays .equals (fields , that .fields );
167
173
}
168
174
169
175
@ Override
@@ -176,36 +182,7 @@ public String toString() {
176
182
return Strings .toString (this , true , true );
177
183
}
178
184
179
- public static class Builder {
180
- private long interval = 0 ;
181
- private List <String > fields ;
182
-
183
- public long getInterval () {
184
- return interval ;
185
- }
186
-
187
- public HistoGroupConfig .Builder setInterval (long interval ) {
188
- this .interval = interval ;
189
- return this ;
190
- }
191
-
192
- public List <String > getFields () {
193
- return fields ;
194
- }
195
-
196
- public HistoGroupConfig .Builder setFields (List <String > fields ) {
197
- this .fields = fields ;
198
- return this ;
199
- }
200
-
201
- public HistoGroupConfig build () {
202
- if (interval <= 0 ) {
203
- throw new IllegalArgumentException ("Parameter [" + INTERVAL .getPreferredName () + "] must be a positive long." );
204
- }
205
- if (fields == null || fields .isEmpty ()) {
206
- throw new IllegalArgumentException ("Parameter [" + FIELDS + "] must have at least one value." );
207
- }
208
- return new HistoGroupConfig (interval , fields .toArray (new String [0 ]));
209
- }
185
+ public static HistogramGroupConfig fromXContent (final XContentParser parser ) throws IOException {
186
+ return PARSER .parse (parser , null );
210
187
}
211
188
}
0 commit comments