2020package org .elasticsearch .client .dataframe .transforms .pivot ;
2121
2222import org .elasticsearch .common .ParseField ;
23+ import org .elasticsearch .common .unit .TimeValue ;
2324import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
2425import org .elasticsearch .common .xcontent .ObjectParser ;
2526import org .elasticsearch .common .xcontent .ToXContentObject ;
3435
3536import static org .elasticsearch .common .xcontent .ConstructingObjectParser .optionalConstructorArg ;
3637
38+ /**
39+ * A grouping via a date histogram aggregation referencing a timefield
40+ */
3741public class DateHistogramGroupSource extends SingleGroupSource implements ToXContentObject {
3842
3943 private static final ParseField TIME_ZONE = new ParseField ("time_zone" );
4044 private static final ParseField FORMAT = new ParseField ("format" );
4145
4246 private static final ConstructingObjectParser <DateHistogramGroupSource , Void > PARSER =
43- new ConstructingObjectParser <>("date_histogram_group_source" , true , (args ) -> new DateHistogramGroupSource ((String ) args [0 ]));
47+ new ConstructingObjectParser <>("date_histogram_group_source" ,
48+ true ,
49+ (args ) -> {
50+ String field = (String )args [0 ];
51+ long interval = 0 ;
52+ DateHistogramInterval dateHistogramInterval = null ;
53+ if (args [1 ] instanceof Long ) {
54+ interval = (Long )args [1 ];
55+ } else {
56+ dateHistogramInterval = (DateHistogramInterval ) args [1 ];
57+ }
58+ ZoneId zoneId = (ZoneId ) args [2 ];
59+ String format = (String ) args [3 ];
60+ return new DateHistogramGroupSource (field , interval , dateHistogramInterval , format , zoneId );
61+ });
4462
4563 static {
4664 PARSER .declareString (optionalConstructorArg (), FIELD );
47- PARSER .declareField ((histogram , interval ) -> {
48- if (interval instanceof Long ) {
49- histogram .setInterval ((long ) interval );
50- } else {
51- histogram .setDateHistogramInterval ((DateHistogramInterval ) interval );
52- }
53- }, p -> {
65+ PARSER .declareField (optionalConstructorArg (), p -> {
5466 if (p .currentToken () == XContentParser .Token .VALUE_NUMBER ) {
5567 return p .longValue ();
5668 } else {
5769 return new DateHistogramInterval (p .text ());
5870 }
5971 }, HistogramGroupSource .INTERVAL , ObjectParser .ValueType .LONG );
60- PARSER .declareField (DateHistogramGroupSource :: setTimeZone , p -> {
72+ PARSER .declareField (optionalConstructorArg () , p -> {
6173 if (p .currentToken () == XContentParser .Token .VALUE_STRING ) {
6274 return ZoneId .of (p .text ());
6375 } else {
6476 return ZoneOffset .ofHours (p .intValue ());
6577 }
6678 }, TIME_ZONE , ObjectParser .ValueType .LONG );
6779
68- PARSER .declareString (DateHistogramGroupSource :: setFormat , FORMAT );
80+ PARSER .declareString (optionalConstructorArg () , FORMAT );
6981 }
7082
7183 public static DateHistogramGroupSource fromXContent (final XContentParser parser ) {
7284 return PARSER .apply (parser , null );
7385 }
7486
75- private long interval = 0 ;
76- private DateHistogramInterval dateHistogramInterval ;
77- private String format ;
78- private ZoneId timeZone ;
87+ private final long interval ;
88+ private final DateHistogramInterval dateHistogramInterval ;
89+ private final String format ;
90+ private final ZoneId timeZone ;
7991
80- public DateHistogramGroupSource (String field ) {
92+ DateHistogramGroupSource (String field , long interval , DateHistogramInterval dateHistogramInterval , String format , ZoneId timeZone ) {
8193 super (field );
94+ this .interval = interval ;
95+ this .dateHistogramInterval = dateHistogramInterval ;
96+ this .format = format ;
97+ this .timeZone = timeZone ;
8298 }
8399
84100 @ Override
@@ -90,40 +106,18 @@ public long getInterval() {
90106 return interval ;
91107 }
92108
93- public void setInterval (long interval ) {
94- if (interval < 1 ) {
95- throw new IllegalArgumentException ("[interval] must be greater than or equal to 1." );
96- }
97- this .interval = interval ;
98- }
99-
100109 public DateHistogramInterval getDateHistogramInterval () {
101110 return dateHistogramInterval ;
102111 }
103112
104- public void setDateHistogramInterval (DateHistogramInterval dateHistogramInterval ) {
105- if (dateHistogramInterval == null ) {
106- throw new IllegalArgumentException ("[dateHistogramInterval] must not be null" );
107- }
108- this .dateHistogramInterval = dateHistogramInterval ;
109- }
110-
111113 public String getFormat () {
112114 return format ;
113115 }
114116
115- public void setFormat (String format ) {
116- this .format = format ;
117- }
118-
119117 public ZoneId getTimeZone () {
120118 return timeZone ;
121119 }
122120
123- public void setTimeZone (ZoneId timeZone ) {
124- this .timeZone = timeZone ;
125- }
126-
127121 @ Override
128122 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
129123 builder .startObject ();
@@ -168,4 +162,88 @@ public boolean equals(Object other) {
168162 public int hashCode () {
169163 return Objects .hash (field , interval , dateHistogramInterval , timeZone , format );
170164 }
165+
166+ public static Builder builder () {
167+ return new Builder ();
168+ }
169+
170+ public static class Builder {
171+
172+ private String field ;
173+ private long interval = 0 ;
174+ private DateHistogramInterval dateHistogramInterval ;
175+ private String format ;
176+ private ZoneId timeZone ;
177+
178+ /**
179+ * The field with which to construct the date histogram grouping
180+ * @param field The field name
181+ * @return The {@link Builder} with the field set.
182+ */
183+ public Builder setField (String field ) {
184+ this .field = field ;
185+ return this ;
186+ }
187+
188+ /**
189+ * Set the interval for the DateHistogram grouping
190+ * @param interval the time interval in milliseconds
191+ * @return the {@link Builder} with the interval set.
192+ */
193+ public Builder setInterval (long interval ) {
194+ if (interval < 1 ) {
195+ throw new IllegalArgumentException ("[interval] must be greater than or equal to 1." );
196+ }
197+ this .interval = interval ;
198+ return this ;
199+ }
200+
201+ /**
202+ * Set the interval for the DateHistogram grouping
203+ * @param timeValue The time value to use as the interval
204+ * @return the {@link Builder} with the interval set.
205+ */
206+ public Builder setInterval (TimeValue timeValue ) {
207+ return setInterval (timeValue .getMillis ());
208+ }
209+
210+ /**
211+ * Sets the interval of the DateHistogram grouping
212+ *
213+ * If this DateHistogramInterval is set, it supersedes the #{@link DateHistogramGroupSource#getInterval()}
214+ * @param dateHistogramInterval the DateHistogramInterval to set
215+ * @return The {@link Builder} with the dateHistogramInterval set.
216+ */
217+ public Builder setDateHistgramInterval (DateHistogramInterval dateHistogramInterval ) {
218+ if (dateHistogramInterval == null ) {
219+ throw new IllegalArgumentException ("[dateHistogramInterval] must not be null" );
220+ }
221+ this .dateHistogramInterval = dateHistogramInterval ;
222+ return this ;
223+ }
224+
225+ /**
226+ * Set the optional String formatting for the time interval.
227+ * @param format The format of the output for the time interval key
228+ * @return The {@link Builder} with the format set.
229+ */
230+ public Builder setFormat (String format ) {
231+ this .format = format ;
232+ return this ;
233+ }
234+
235+ /**
236+ * Sets the time zone to use for this aggregation
237+ * @param timeZone The zoneId for the timeZone
238+ * @return The {@link Builder} with the timeZone set.
239+ */
240+ public Builder setTimeZone (ZoneId timeZone ) {
241+ this .timeZone = timeZone ;
242+ return this ;
243+ }
244+
245+ public DateHistogramGroupSource build () {
246+ return new DateHistogramGroupSource (field , interval , dateHistogramInterval , format , timeZone );
247+ }
248+ }
171249}
0 commit comments