@@ -72,9 +72,8 @@ public class HistogramAggregationBuilder extends ValuesSourceAggregationBuilder<
7272
7373 PARSER .declareLong (HistogramAggregationBuilder ::minDocCount , Histogram .MIN_DOC_COUNT_FIELD );
7474
75- PARSER .declareField ((histogram , extendedBounds ) -> {
76- histogram .extendedBounds (extendedBounds [0 ], extendedBounds [1 ]);
77- }, parser -> EXTENDED_BOUNDS_PARSER .apply (parser , null ), Histogram .EXTENDED_BOUNDS_FIELD , ObjectParser .ValueType .OBJECT );
75+ PARSER .declareField (HistogramAggregationBuilder ::extendedBounds , parser -> DoubleBounds .PARSER .apply (parser , null ),
76+ Histogram .EXTENDED_BOUNDS_FIELD , ObjectParser .ValueType .OBJECT );
7877
7978 PARSER .declareField (HistogramAggregationBuilder ::hardBounds , parser -> DoubleBounds .PARSER .apply (parser , null ),
8079 Histogram .HARD_BOUNDS_FIELD , ObjectParser .ValueType .OBJECT );
@@ -89,9 +88,7 @@ public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
8988
9089 private double interval ;
9190 private double offset = 0 ;
92- //TODO: Replace with DoubleBounds
93- private double minBound = Double .POSITIVE_INFINITY ;
94- private double maxBound = Double .NEGATIVE_INFINITY ;
91+ private DoubleBounds extendedBounds ;
9592 private DoubleBounds hardBounds ;
9693 private BucketOrder order = BucketOrder .key (true );
9794 private boolean keyed = false ;
@@ -113,8 +110,7 @@ protected HistogramAggregationBuilder(HistogramAggregationBuilder clone,
113110 super (clone , factoriesBuilder , metadata );
114111 this .interval = clone .interval ;
115112 this .offset = clone .offset ;
116- this .minBound = clone .minBound ;
117- this .maxBound = clone .maxBound ;
113+ this .extendedBounds = clone .extendedBounds ;
118114 this .hardBounds = clone .hardBounds ;
119115 this .order = clone .order ;
120116 this .keyed = clone .keyed ;
@@ -134,10 +130,17 @@ public HistogramAggregationBuilder(StreamInput in) throws IOException {
134130 minDocCount = in .readVLong ();
135131 interval = in .readDouble ();
136132 offset = in .readDouble ();
137- minBound = in .readDouble ();
138- maxBound = in .readDouble ();
139133 if (in .getVersion ().onOrAfter (Version .V_7_10_0 )) {
134+ extendedBounds = in .readOptionalWriteable (DoubleBounds ::new );
140135 hardBounds = in .readOptionalWriteable (DoubleBounds ::new );
136+ } else {
137+ double minBound = in .readDouble ();
138+ double maxBound = in .readDouble ();
139+ if (minBound == Double .POSITIVE_INFINITY && maxBound == Double .NEGATIVE_INFINITY ) {
140+ extendedBounds = null ;
141+ } else {
142+ extendedBounds = new DoubleBounds (minBound , maxBound );
143+ }
141144 }
142145 }
143146
@@ -148,10 +151,17 @@ protected void innerWriteTo(StreamOutput out) throws IOException {
148151 out .writeVLong (minDocCount );
149152 out .writeDouble (interval );
150153 out .writeDouble (offset );
151- out .writeDouble (minBound );
152- out .writeDouble (maxBound );
153154 if (out .getVersion ().onOrAfter (Version .V_7_10_0 )) {
155+ out .writeOptionalWriteable (extendedBounds );
154156 out .writeOptionalWriteable (hardBounds );
157+ } else {
158+ if (extendedBounds != null ) {
159+ out .writeDouble (extendedBounds .getMin ());
160+ out .writeDouble (extendedBounds .getMax ());
161+ } else {
162+ out .writeDouble (Double .POSITIVE_INFINITY );
163+ out .writeDouble (Double .NEGATIVE_INFINITY );
164+ }
155165 }
156166 }
157167
@@ -182,12 +192,16 @@ public HistogramAggregationBuilder offset(double offset) {
182192
183193 /** Get the current minimum bound that is set on this builder. */
184194 public double minBound () {
185- return minBound ;
195+ return extendedBounds . getMin () ;
186196 }
187197
188198 /** Get the current maximum bound that is set on this builder. */
189199 public double maxBound () {
190- return maxBound ;
200+ return extendedBounds .getMax ();
201+ }
202+
203+ protected DoubleBounds extendedBounds () {
204+ return extendedBounds ;
191205 }
192206
193207 /**
@@ -200,17 +214,23 @@ public double maxBound() {
200214 * are not finite.
201215 */
202216 public HistogramAggregationBuilder extendedBounds (double minBound , double maxBound ) {
203- if (Double .isFinite (minBound ) == false ) {
204- throw new IllegalArgumentException ("minBound must be finite, got: " + minBound );
205- }
206- if (Double .isFinite (maxBound ) == false ) {
207- throw new IllegalArgumentException ("maxBound must be finite, got: " + maxBound );
208- }
209- if (maxBound < minBound ) {
210- throw new IllegalArgumentException ("maxBound [" + maxBound + "] must be greater than minBound [" + minBound + "]" );
217+ return extendedBounds (new DoubleBounds (minBound , maxBound ));
218+ }
219+
220+ /**
221+ * Set extended bounds on this builder: buckets between {@code minBound} and
222+ * {@code maxBound} will be created even if no documents fell into these
223+ * buckets.
224+ *
225+ * @throws IllegalArgumentException
226+ * if maxBound is less that minBound, or if either of the bounds
227+ * are not finite.
228+ */
229+ public HistogramAggregationBuilder extendedBounds (DoubleBounds extendedBounds ) {
230+ if (extendedBounds == null ) {
231+ throw new IllegalArgumentException ("[extended_bounds] must not be null: [" + name + "]" );
211232 }
212- this .minBound = minBound ;
213- this .maxBound = maxBound ;
233+ this .extendedBounds = extendedBounds ;
214234 return this ;
215235 }
216236
@@ -307,14 +327,15 @@ protected XContentBuilder doXContentBody(XContentBuilder builder, Params params)
307327
308328 builder .field (Histogram .MIN_DOC_COUNT_FIELD .getPreferredName (), minDocCount );
309329
310- if (Double . isFinite ( minBound ) || Double . isFinite ( maxBound ) ) {
330+ if (extendedBounds != null ) {
311331 builder .startObject (Histogram .EXTENDED_BOUNDS_FIELD .getPreferredName ());
312- if (Double .isFinite (minBound )) {
313- builder .field ("min" , minBound );
314- }
315- if (Double .isFinite (maxBound )) {
316- builder .field ("max" , maxBound );
317- }
332+ extendedBounds .toXContent (builder , params );
333+ builder .endObject ();
334+ }
335+
336+ if (hardBounds != null ) {
337+ builder .startObject (Histogram .HARD_BOUNDS_FIELD .getPreferredName ());
338+ hardBounds .toXContent (builder , params );
318339 builder .endObject ();
319340 }
320341
@@ -332,23 +353,23 @@ protected ValuesSourceAggregatorFactory innerBuild(QueryShardContext queryShardC
332353 AggregatorFactory parent ,
333354 AggregatorFactories .Builder subFactoriesBuilder ) throws IOException {
334355
335- if (hardBounds != null ) {
336- if (hardBounds .getMax () != null && hardBounds .getMax () < maxBound ) {
356+ if (hardBounds != null && extendedBounds != null ) {
357+ if (hardBounds .getMax () != null && extendedBounds . getMax () != null && hardBounds .getMax () < extendedBounds . getMax () ) {
337358 throw new IllegalArgumentException ("Extended bounds have to be inside hard bounds, hard bounds: [" +
338- hardBounds + "], extended bounds: [" + minBound + "--" + maxBound + "]" );
359+ hardBounds + "], extended bounds: [" + extendedBounds . getMin () + "--" + extendedBounds . getMax () + "]" );
339360 }
340- if (hardBounds .getMin () != null && hardBounds .getMin () > minBound ) {
361+ if (hardBounds .getMin () != null && extendedBounds . getMin () != null && hardBounds .getMin () > extendedBounds . getMin () ) {
341362 throw new IllegalArgumentException ("Extended bounds have to be inside hard bounds, hard bounds: [" +
342- hardBounds + "], extended bounds: [" + minBound + "--" + maxBound + "]" );
363+ hardBounds + "], extended bounds: [" + extendedBounds . getMin () + "--" + extendedBounds . getMax () + "]" );
343364 }
344365 }
345- return new HistogramAggregatorFactory (name , config , interval , offset , order , keyed , minDocCount , minBound , maxBound ,
366+ return new HistogramAggregatorFactory (name , config , interval , offset , order , keyed , minDocCount , extendedBounds ,
346367 hardBounds , queryShardContext , parent , subFactoriesBuilder , metadata );
347368 }
348369
349370 @ Override
350371 public int hashCode () {
351- return Objects .hash (super .hashCode (), order , keyed , minDocCount , interval , offset , minBound , maxBound , hardBounds );
372+ return Objects .hash (super .hashCode (), order , keyed , minDocCount , interval , offset , extendedBounds , hardBounds );
352373 }
353374
354375 @ Override
@@ -362,8 +383,7 @@ public boolean equals(Object obj) {
362383 && Objects .equals (minDocCount , other .minDocCount )
363384 && Objects .equals (interval , other .interval )
364385 && Objects .equals (offset , other .offset )
365- && Objects .equals (minBound , other .minBound )
366- && Objects .equals (maxBound , other .maxBound )
386+ && Objects .equals (extendedBounds , other .extendedBounds )
367387 && Objects .equals (hardBounds , other .hardBounds );
368388 }
369389}
0 commit comments