11package org .opendatasoft .elasticsearch .search .aggregations .bucket ;
22
33import org .elasticsearch .Version ;
4- import org .elasticsearch .xcontent .ParseField ;
54import org .elasticsearch .common .Rounding ;
65import org .elasticsearch .common .io .stream .StreamInput ;
76import org .elasticsearch .common .io .stream .StreamOutput ;
87import org .elasticsearch .common .io .stream .Writeable ;
98import org .elasticsearch .common .time .DateFormatter ;
10- import org .elasticsearch .xcontent .ObjectParser ;
11- import org .elasticsearch .xcontent .XContentBuilder ;
12- import org .elasticsearch .xcontent .XContentParser ;
139import org .elasticsearch .index .mapper .DateFieldMapper ;
14- import org .elasticsearch .search .aggregations .support .AggregationContext ;
1510import org .elasticsearch .search .DocValueFormat ;
1611import org .elasticsearch .search .aggregations .AggregationBuilder ;
1712import org .elasticsearch .search .aggregations .AggregatorFactories .Builder ;
1813import org .elasticsearch .search .aggregations .AggregatorFactory ;
1914import org .elasticsearch .search .aggregations .BucketOrder ;
2015import org .elasticsearch .search .aggregations .InternalOrder ;
16+ import org .elasticsearch .search .aggregations .support .AggregationContext ;
2117import org .elasticsearch .search .aggregations .support .CoreValuesSourceType ;
2218import org .elasticsearch .search .aggregations .support .ValuesSourceAggregationBuilder ;
2319import org .elasticsearch .search .aggregations .support .ValuesSourceAggregatorFactory ;
2420import org .elasticsearch .search .aggregations .support .ValuesSourceConfig ;
2521import org .elasticsearch .search .aggregations .support .ValuesSourceRegistry ;
2622import org .elasticsearch .search .aggregations .support .ValuesSourceType ;
23+ import org .elasticsearch .xcontent .ObjectParser ;
24+ import org .elasticsearch .xcontent .ParseField ;
25+ import org .elasticsearch .xcontent .XContentBuilder ;
26+ import org .elasticsearch .xcontent .XContentParser ;
2727
2828import java .io .IOException ;
2929import java .time .ZoneId ;
3636
3737import static java .util .Collections .unmodifiableMap ;
3838
39-
4039/**
4140 * The builder of the aggregatorFactory. Also implements the parsing of the request.
4241 */
4342public class DateHierarchyAggregationBuilder extends ValuesSourceAggregationBuilder <DateHierarchyAggregationBuilder > {
4443 public static final String NAME = "date_hierarchy" ;
4544 public static final ValuesSourceRegistry .RegistryKey <DateHierarchyAggregationSupplier > REGISTRY_KEY =
46- new ValuesSourceRegistry .RegistryKey <>(NAME , DateHierarchyAggregationSupplier .class );
47-
45+ new ValuesSourceRegistry .RegistryKey <>(NAME , DateHierarchyAggregationSupplier .class );
4846
4947 public static final ParseField INTERVAL_FIELD = new ParseField ("interval" );
5048 public static final ParseField ORDER_FIELD = new ParseField ("order" );
5149 public static final ParseField SIZE_FIELD = new ParseField ("size" );
5250 public static final ParseField SHARD_SIZE_FIELD = new ParseField ("shard_size" );
5351 public static final ParseField MIN_DOC_COUNT_FIELD = new ParseField ("min_doc_count" );
5452
55-
5653 public static final Map <String , IntervalConfig > INTERVAL_CONFIG ;
5754 static {
5855 Map <String , IntervalConfig > dateFieldUnits = new LinkedHashMap <>();
@@ -88,13 +85,19 @@ public PreparedRounding(RoundingInfo roundingInfo, Rounding.Prepared prepared) {
8885 public List <PreparedRounding > buildRoundings () {
8986 List <PreparedRounding > roundings = new ArrayList <>();
9087
91- ZoneId timeZone = timeZone () == null ? ZoneOffset .UTC : timeZone ();
88+ ZoneId timeZone = timeZone () == null ? ZoneOffset .UTC : timeZone ();
9289
9390 long now = System .currentTimeMillis ();
9491 for (String interval : INTERVAL_CONFIG .keySet ()) {
95- RoundingInfo ri = new RoundingInfo (interval , createRounding (INTERVAL_CONFIG .get (interval ).dateTimeUnit ),
96- new DocValueFormat .DateTime (DateFormatter .forPattern (INTERVAL_CONFIG .get (interval ).format ), timeZone ,
97- DateFieldMapper .Resolution .MILLISECONDS ));
92+ RoundingInfo ri = new RoundingInfo (
93+ interval ,
94+ createRounding (INTERVAL_CONFIG .get (interval ).dateTimeUnit ),
95+ new DocValueFormat .DateTime (
96+ DateFormatter .forPattern (INTERVAL_CONFIG .get (interval ).format ),
97+ timeZone ,
98+ DateFieldMapper .Resolution .MILLISECONDS
99+ )
100+ );
98101 roundings .add (new PreparedRounding (ri , ri .rounding .prepareForUnknown ()));
99102
100103 if (interval .equals (interval ())) {
@@ -112,7 +115,7 @@ public static class RoundingInfo implements Writeable {
112115
113116 public RoundingInfo (String interval , Rounding rounding , DocValueFormat docValueFormat ) {
114117 this .interval = interval ;
115- this .rounding = rounding ;
118+ this .rounding = rounding ;
116119 this .format = docValueFormat ;
117120 }
118121
@@ -130,10 +133,12 @@ public void writeTo(StreamOutput out) throws IOException {
130133 }
131134 }
132135
133- public static final DateHierarchyAggregator .BucketCountThresholds DEFAULT_BUCKET_COUNT_THRESHOLDS = new
134- DateHierarchyAggregator .BucketCountThresholds (10 , -1 );
135- public static final ObjectParser <DateHierarchyAggregationBuilder , String > PARSER =
136- ObjectParser .fromBuilder (NAME , DateHierarchyAggregationBuilder ::new );
136+ public static final DateHierarchyAggregator .BucketCountThresholds DEFAULT_BUCKET_COUNT_THRESHOLDS =
137+ new DateHierarchyAggregator .BucketCountThresholds (10 , -1 );
138+ public static final ObjectParser <DateHierarchyAggregationBuilder , String > PARSER = ObjectParser .fromBuilder (
139+ NAME ,
140+ DateHierarchyAggregationBuilder ::new
141+ );
137142 static {
138143
139144 ValuesSourceAggregationBuilder .declareFields (PARSER , true , true , true );
@@ -151,8 +156,7 @@ public void writeTo(StreamOutput out) throws IOException {
151156 PARSER .declareInt (DateHierarchyAggregationBuilder ::size , SIZE_FIELD );
152157 PARSER .declareLong (DateHierarchyAggregationBuilder ::minDocCount , MIN_DOC_COUNT_FIELD );
153158 PARSER .declareInt (DateHierarchyAggregationBuilder ::shardSize , SHARD_SIZE_FIELD );
154- PARSER .declareObjectArray (DateHierarchyAggregationBuilder ::order , (p , c ) -> InternalOrder .Parser .parseOrderParam (p ),
155- ORDER_FIELD );
159+ PARSER .declareObjectArray (DateHierarchyAggregationBuilder ::order , (p , c ) -> InternalOrder .Parser .parseOrderParam (p ), ORDER_FIELD );
156160 }
157161
158162 public static AggregationBuilder parse (String aggregationName , XContentParser parser ) throws IOException {
@@ -168,8 +172,8 @@ public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
168172 private String interval = "years" ;
169173 private BucketOrder order = BucketOrder .compound (BucketOrder .count (false )); // automatically adds tie-breaker key asc order
170174 private DateHierarchyAggregator .BucketCountThresholds bucketCountThresholds = new DateHierarchyAggregator .BucketCountThresholds (
171- DEFAULT_BUCKET_COUNT_THRESHOLDS );
172-
175+ DEFAULT_BUCKET_COUNT_THRESHOLDS
176+ );
173177
174178 private DateHierarchyAggregationBuilder (String name ) {
175179 super (name );
@@ -193,8 +197,7 @@ public DateHierarchyAggregationBuilder(StreamInput in) throws IOException {
193197 timeZone = in .readOptionalZoneId ();
194198 }
195199
196- private DateHierarchyAggregationBuilder (DateHierarchyAggregationBuilder clone , Builder factoriesBuilder ,
197- Map <String , Object > metaData ) {
200+ private DateHierarchyAggregationBuilder (DateHierarchyAggregationBuilder clone , Builder factoriesBuilder , Map <String , Object > metaData ) {
198201 super (clone , factoriesBuilder , metaData );
199202 order = clone .order ;
200203 minDocCount = clone .minDocCount ;
@@ -275,7 +278,7 @@ private DateHierarchyAggregationBuilder order(BucketOrder order) {
275278 if (order == null ) {
276279 throw new IllegalArgumentException ("[order] must not be null: [" + name + "]" );
277280 }
278- if (order instanceof InternalOrder .CompoundOrder || InternalOrder .isKeyOrder (order )) {
281+ if (order instanceof InternalOrder .CompoundOrder || InternalOrder .isKeyOrder (order )) {
279282 this .order = order ; // if order already contains a tie-breaker we are good to go
280283 } else { // otherwise add a tie-breaker by using a compound order
281284 this .order = BucketOrder .compound (order );
@@ -292,7 +295,6 @@ private DateHierarchyAggregationBuilder order(List<BucketOrder> orders) {
292295 return this ;
293296 }
294297
295-
296298 /**
297299 * Sets the size - indicating how many term buckets should be returned
298300 * (defaults to 10)
@@ -310,7 +312,8 @@ public DateHierarchyAggregationBuilder size(int size) {
310312 public DateHierarchyAggregationBuilder minDocCount (long minDocCount ) {
311313 if (minDocCount < 0 ) {
312314 throw new IllegalArgumentException (
313- "[minDocCount] must be greater than or equal to 0. Found [" + minDocCount + "] in [" + name + "]" );
315+ "[minDocCount] must be greater than or equal to 0. Found [" + minDocCount + "] in [" + name + "]"
316+ );
314317 }
315318 this .minDocCount = minDocCount ;
316319 return this ;
@@ -336,8 +339,7 @@ public BucketCardinality bucketCardinality() {
336339 */
337340 public DateHierarchyAggregationBuilder shardSize (int shardSize ) {
338341 if (shardSize <= 0 ) {
339- throw new IllegalArgumentException (
340- "[shardSize] must be greater than 0. Found [" + shardSize + "] in [" + name + "]" );
342+ throw new IllegalArgumentException ("[shardSize] must be greater than 0. Found [" + shardSize + "] in [" + name + "]" );
341343 }
342344 bucketCountThresholds .setShardSize (shardSize );
343345 return this ;
@@ -351,25 +353,27 @@ public int shardSize() {
351353 }
352354
353355 @ Override
354- protected ValuesSourceAggregatorFactory innerBuild (AggregationContext context ,
355- ValuesSourceConfig config ,
356- AggregatorFactory parent ,
357- Builder subFactoriesBuilder ) throws IOException {
358-
356+ protected ValuesSourceAggregatorFactory innerBuild (
357+ AggregationContext context ,
358+ ValuesSourceConfig config ,
359+ AggregatorFactory parent ,
360+ Builder subFactoriesBuilder
361+ ) throws IOException {
359362
360363 final List <PreparedRounding > preparedRoundings = buildRoundings ();
361364
362365 return new DateHierarchyAggregatorFactory (
363- name ,
364- config ,
365- order ,
366- preparedRoundings ,
367- minDocCount ,
368- bucketCountThresholds ,
369- context ,
370- parent ,
371- subFactoriesBuilder ,
372- metadata );
366+ name ,
367+ config ,
368+ order ,
369+ preparedRoundings ,
370+ minDocCount ,
371+ bucketCountThresholds ,
372+ context ,
373+ parent ,
374+ subFactoriesBuilder ,
375+ metadata
376+ );
373377 }
374378
375379 @ Override
@@ -398,10 +402,10 @@ public boolean equals(Object obj) {
398402 if (!super .equals (obj )) return false ;
399403 DateHierarchyAggregationBuilder other = (DateHierarchyAggregationBuilder ) obj ;
400404 return Objects .equals (interval , other .interval )
401- && Objects .equals (order , other .order )
402- && Objects .equals (minDocCount , other .minDocCount )
403- && Objects .equals (bucketCountThresholds , other .bucketCountThresholds )
404- && Objects .equals (timeZone , other .timeZone );
405+ && Objects .equals (order , other .order )
406+ && Objects .equals (minDocCount , other .minDocCount )
407+ && Objects .equals (bucketCountThresholds , other .bucketCountThresholds )
408+ && Objects .equals (timeZone , other .timeZone );
405409 }
406410
407411 @ Override
@@ -410,6 +414,7 @@ public String getType() {
410414 }
411415
412416 @ Override
413- protected ValuesSourceRegistry .RegistryKey <?> getRegistryKey () { return REGISTRY_KEY ; }
417+ protected ValuesSourceRegistry .RegistryKey <?> getRegistryKey () {
418+ return REGISTRY_KEY ;
419+ }
414420}
415-
0 commit comments