20
20
package org .elasticsearch .client .transform .transforms .pivot ;
21
21
22
22
import org .elasticsearch .common .ParseField ;
23
+ import org .elasticsearch .common .Strings ;
23
24
import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
24
25
import org .elasticsearch .common .xcontent .ObjectParser ;
25
26
import org .elasticsearch .common .xcontent .ToXContentFragment ;
26
27
import org .elasticsearch .common .xcontent .ToXContentObject ;
27
28
import org .elasticsearch .common .xcontent .XContentBuilder ;
28
29
import org .elasticsearch .common .xcontent .XContentParser ;
30
+ import org .elasticsearch .script .Script ;
29
31
import org .elasticsearch .search .aggregations .bucket .histogram .DateHistogramInterval ;
30
32
31
33
import java .io .IOException ;
@@ -48,23 +50,28 @@ public class DateHistogramGroupSource extends SingleGroupSource implements ToXCo
48
50
49
51
// From DateHistogramAggregationBuilder in core, transplanted and modified to a set
50
52
// so we don't need to import a dependency on the class
51
- private static final Set <String > DATE_FIELD_UNITS = Collections .unmodifiableSet (new HashSet <>(Arrays .asList (
52
- "year" ,
53
- "1y" ,
54
- "quarter" ,
55
- "1q" ,
56
- "month" ,
57
- "1M" ,
58
- "week" ,
59
- "1w" ,
60
- "day" ,
61
- "1d" ,
62
- "hour" ,
63
- "1h" ,
64
- "minute" ,
65
- "1m" ,
66
- "second" ,
67
- "1s" )));
53
+ private static final Set <String > DATE_FIELD_UNITS = Collections .unmodifiableSet (
54
+ new HashSet <>(
55
+ Arrays .asList (
56
+ "year" ,
57
+ "1y" ,
58
+ "quarter" ,
59
+ "1q" ,
60
+ "month" ,
61
+ "1M" ,
62
+ "week" ,
63
+ "1w" ,
64
+ "day" ,
65
+ "1d" ,
66
+ "hour" ,
67
+ "1h" ,
68
+ "minute" ,
69
+ "1m" ,
70
+ "second" ,
71
+ "1s"
72
+ )
73
+ )
74
+ );
68
75
69
76
/**
70
77
* Interval can be specified in 2 ways:
@@ -76,6 +83,7 @@ public class DateHistogramGroupSource extends SingleGroupSource implements ToXCo
76
83
*/
77
84
public interface Interval extends ToXContentFragment {
78
85
String getName ();
86
+
79
87
DateHistogramInterval getInterval ();
80
88
}
81
89
@@ -131,8 +139,9 @@ public static class CalendarInterval implements Interval {
131
139
public CalendarInterval (DateHistogramInterval interval ) {
132
140
this .interval = interval ;
133
141
if (DATE_FIELD_UNITS .contains (interval .toString ()) == false ) {
134
- throw new IllegalArgumentException ("The supplied interval [" + interval + "] could not be parsed " +
135
- "as a calendar interval." );
142
+ throw new IllegalArgumentException (
143
+ "The supplied interval [" + interval + "] could not be parsed " + "as a calendar interval."
144
+ );
136
145
}
137
146
}
138
147
@@ -173,33 +182,35 @@ public int hashCode() {
173
182
}
174
183
}
175
184
176
- private static final ConstructingObjectParser <DateHistogramGroupSource , Void > PARSER =
177
- new ConstructingObjectParser <>("date_histogram_group_source" ,
178
- true ,
179
- (args ) -> {
180
- String field = (String )args [0 ];
181
- String fixedInterval = (String ) args [1 ];
182
- String calendarInterval = (String ) args [2 ];
183
-
184
- Interval interval = null ;
185
-
186
- if (fixedInterval != null && calendarInterval != null ) {
187
- throw new IllegalArgumentException ("You must specify either fixed_interval or calendar_interval, found both" );
188
- } else if (fixedInterval != null ) {
189
- interval = new FixedInterval (new DateHistogramInterval (fixedInterval ));
190
- } else if (calendarInterval != null ) {
191
- interval = new CalendarInterval (new DateHistogramInterval (calendarInterval ));
192
- } else {
193
- throw new IllegalArgumentException ("You must specify either fixed_interval or calendar_interval, found none" );
194
- }
195
-
196
- ZoneId zoneId = (ZoneId ) args [3 ];
197
- return new DateHistogramGroupSource (field , interval , zoneId );
198
- });
185
+ private static final ConstructingObjectParser <DateHistogramGroupSource , Void > PARSER = new ConstructingObjectParser <>(
186
+ "date_histogram_group_source" ,
187
+ true ,
188
+ (args ) -> {
189
+ String field = (String ) args [0 ];
190
+ Script script = (Script ) args [1 ];
191
+ String fixedInterval = (String ) args [2 ];
192
+ String calendarInterval = (String ) args [3 ];
193
+ ZoneId zoneId = (ZoneId ) args [4 ];
194
+
195
+ Interval interval = null ;
196
+
197
+ if (fixedInterval != null && calendarInterval != null ) {
198
+ throw new IllegalArgumentException ("You must specify either fixed_interval or calendar_interval, found both" );
199
+ } else if (fixedInterval != null ) {
200
+ interval = new FixedInterval (new DateHistogramInterval (fixedInterval ));
201
+ } else if (calendarInterval != null ) {
202
+ interval = new CalendarInterval (new DateHistogramInterval (calendarInterval ));
203
+ } else {
204
+ throw new IllegalArgumentException ("You must specify either fixed_interval or calendar_interval, found none" );
205
+ }
206
+
207
+ return new DateHistogramGroupSource (field , script , interval , zoneId );
208
+ }
209
+ );
199
210
200
211
static {
201
212
PARSER .declareString (optionalConstructorArg (), FIELD );
202
-
213
+ Script . declareScript ( PARSER , optionalConstructorArg (), SCRIPT );
203
214
PARSER .declareString (optionalConstructorArg (), new ParseField (FixedInterval .NAME ));
204
215
PARSER .declareString (optionalConstructorArg (), new ParseField (CalendarInterval .NAME ));
205
216
@@ -219,8 +230,8 @@ public static DateHistogramGroupSource fromXContent(final XContentParser parser)
219
230
private final Interval interval ;
220
231
private final ZoneId timeZone ;
221
232
222
- DateHistogramGroupSource (String field , Interval interval , ZoneId timeZone ) {
223
- super (field );
233
+ DateHistogramGroupSource (String field , Script script , Interval interval , ZoneId timeZone ) {
234
+ super (field , script );
224
235
this .interval = interval ;
225
236
this .timeZone = timeZone ;
226
237
}
@@ -241,9 +252,7 @@ public ZoneId getTimeZone() {
241
252
@ Override
242
253
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
243
254
builder .startObject ();
244
- if (field != null ) {
245
- builder .field (FIELD .getPreferredName (), field );
246
- }
255
+ super .innerXContent (builder , params );
247
256
interval .toXContent (builder , params );
248
257
if (timeZone != null ) {
249
258
builder .field (TIME_ZONE .getPreferredName (), timeZone .toString ());
@@ -264,23 +273,29 @@ public boolean equals(Object other) {
264
273
265
274
final DateHistogramGroupSource that = (DateHistogramGroupSource ) other ;
266
275
267
- return Objects .equals (this .field , that .field ) &&
268
- Objects .equals (this .interval , that .interval ) &&
269
- Objects .equals (this .timeZone , that .timeZone );
276
+ return Objects .equals (this .field , that .field )
277
+ && Objects .equals (this .interval , that .interval )
278
+ && Objects .equals (this .timeZone , that .timeZone );
270
279
}
271
280
272
281
@ Override
273
282
public int hashCode () {
274
283
return Objects .hash (field , interval , timeZone );
275
284
}
276
285
286
+ @ Override
287
+ public String toString () {
288
+ return Strings .toString (this , true , true );
289
+ }
290
+
277
291
public static Builder builder () {
278
292
return new Builder ();
279
293
}
280
294
281
295
public static class Builder {
282
296
283
297
private String field ;
298
+ private Script script ;
284
299
private Interval interval ;
285
300
private ZoneId timeZone ;
286
301
@@ -294,6 +309,16 @@ public Builder setField(String field) {
294
309
return this ;
295
310
}
296
311
312
+ /**
313
+ * The script with which to construct the date histogram grouping
314
+ * @param script The script
315
+ * @return The {@link Builder} with the script set.
316
+ */
317
+ public Builder setScript (Script script ) {
318
+ this .script = script ;
319
+ return this ;
320
+ }
321
+
297
322
/**
298
323
* Set the interval for the DateHistogram grouping
299
324
* @param interval a fixed or calendar interval
@@ -315,7 +340,7 @@ public Builder setTimeZone(ZoneId timeZone) {
315
340
}
316
341
317
342
public DateHistogramGroupSource build () {
318
- return new DateHistogramGroupSource (field , interval , timeZone );
343
+ return new DateHistogramGroupSource (field , script , interval , timeZone );
319
344
}
320
345
}
321
346
}
0 commit comments