1212import org .elasticsearch .common .io .stream .StreamInput ;
1313import org .elasticsearch .common .io .stream .StreamOutput ;
1414import 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 ;
1717import org .elasticsearch .common .xcontent .XContentBuilder ;
18+ import org .elasticsearch .common .xcontent .XContentParser ;
1819import org .elasticsearch .search .aggregations .bucket .composite .CompositeValuesSourceBuilder ;
1920import org .elasticsearch .search .aggregations .bucket .composite .HistogramValuesSourceBuilder ;
2021import org .elasticsearch .search .aggregations .bucket .histogram .HistogramAggregationBuilder ;
2728import java .util .List ;
2829import java .util .Map ;
2930import java .util .Objects ;
30- import java .util .Set ;
3131import java .util .stream .Collectors ;
3232
33+ import static org .elasticsearch .common .xcontent .ConstructingObjectParser .constructorArg ;
34+
3335/**
3436 * The configuration object for the histograms in the rollup config
3537 *
4244 * ]
4345 * }
4446 */
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 {
4948
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+ }
5261
5362 private final long interval ;
5463 private final String [] fields ;
5564
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+ }
6272 this .interval = interval ;
6373 this .fields = fields ;
6474 }
6575
66- HistoGroupConfig ( StreamInput in ) throws IOException {
76+ HistogramGroupConfig ( final StreamInput in ) throws IOException {
6777 interval = in .readVLong ();
6878 fields = in .readStringArray ();
6979 }
@@ -101,18 +111,14 @@ public List<CompositeValuesSourceBuilder<?>> toBuilders() {
101111 public Map <String , Object > toAggCap () {
102112 Map <String , Object > map = new HashMap <>(2 );
103113 map .put ("agg" , HistogramAggregationBuilder .NAME );
104- map .put (INTERVAL . getPreferredName () , interval );
114+ map .put (INTERVAL , interval );
105115 return map ;
106116 }
107117
108118 public Map <String , Object > getMetadata () {
109119 return Collections .singletonMap (RollupField .formatMetaField (RollupField .INTERVAL ), interval );
110120 }
111121
112- public Set <String > getAllFields () {
113- return Arrays .stream (fields ).collect (Collectors .toSet ());
114- }
115-
116122 public void validateMappings (Map <String , Map <String , FieldCapabilities >> fieldCapsResponse ,
117123 ActionRequestValidationException validationException ) {
118124
@@ -138,9 +144,13 @@ public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa
138144 }
139145
140146 @ 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 ();
144154 return builder ;
145155 }
146156
@@ -151,19 +161,15 @@ public void writeTo(StreamOutput out) throws IOException {
151161 }
152162
153163 @ Override
154- public boolean equals (Object other ) {
164+ public boolean equals (final Object other ) {
155165 if (this == other ) {
156166 return true ;
157167 }
158-
159168 if (other == null || getClass () != other .getClass ()) {
160169 return false ;
161170 }
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 );
167173 }
168174
169175 @ Override
@@ -176,36 +182,7 @@ public String toString() {
176182 return Strings .toString (this , true , true );
177183 }
178184
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 );
210187 }
211188}
0 commit comments