diff --git a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/40_range.yml b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/40_range.yml index 03056b4c81aa..880772422506 100644 --- a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/40_range.yml +++ b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/40_range.yml @@ -124,8 +124,8 @@ setup: --- "Float range": - skip: - version: " - 7.16.99" - reason: Bug fixed in 8.1.0 and backported to 7.17.0 + version: " - 8.1.0" + reason: Bug fixed in 8.1.0 - do: search: index: test diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java index f253b9552027..d78aa5dde161 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregationBuilder.java @@ -364,7 +364,7 @@ protected DateRangeAggregatorFactory innerBuild( } else if (Double.isFinite(to)) { to = parser.parseDouble(Long.toString((long) to), false, context::nowInMillis); } - return new RangeAggregator.Range(range.getKey(), from, from, fromAsString, to, to, toAsString); + return new RangeAggregator.Range(range.getKey(), from, fromAsString, to, toAsString); }); if (ranges.length == 0) { throw new IllegalArgumentException("No [ranges] specified for the [" + this.getName() + "] aggregation"); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalBinaryRange.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalBinaryRange.java index d95bec4c0cd6..6a8ae36b49a5 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalBinaryRange.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalBinaryRange.java @@ -9,6 +9,7 @@ package org.elasticsearch.search.aggregations.bucket.range; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.search.DocValueFormat; @@ -54,7 +55,7 @@ public Bucket( ) { this.format = format; this.keyed = keyed; - this.key = key != null ? key : generateKey(from, to, format); + this.key = key; this.from = from; this.to = to; this.docCount = docCount; @@ -69,7 +70,7 @@ private static String generateKey(BytesRef from, BytesRef to, DocValueFormat for } private static Bucket createFromStream(StreamInput in, DocValueFormat format, boolean keyed) throws IOException { - String key = in.readString(); + String key = in.getVersion().onOrAfter(Version.V_8_1_0) ? in.readOptionalString() : in.readString(); BytesRef from = in.readBoolean() ? in.readBytesRef() : null; BytesRef to = in.readBoolean() ? in.readBytesRef() : null; long docCount = in.readLong(); @@ -80,7 +81,11 @@ private static Bucket createFromStream(StreamInput in, DocValueFormat format, bo @Override public void writeTo(StreamOutput out) throws IOException { - out.writeString(key); + if (out.getVersion().onOrAfter(Version.V_8_1_0)) { + out.writeOptionalString(key); + } else { + out.writeString(key == null ? generateKey(from, to, format) : key); + } out.writeBoolean(from != null); if (from != null) { out.writeBytesRef(from); @@ -95,12 +100,12 @@ public void writeTo(StreamOutput out) throws IOException { @Override public Object getKey() { - return key; + return getKeyAsString(); } @Override public String getKeyAsString() { - return key; + return this.key == null ? generateKey(this.from, this.to, this.format) : this.key; } @Override @@ -115,7 +120,7 @@ public Aggregations getAggregations() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - String key = this.key; + final String key = getKeyAsString(); if (keyed) { builder.startObject(key); } else { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRange.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRange.java index 031b1777acf0..999d37e1fe65 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRange.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRange.java @@ -28,29 +28,25 @@ public static class Bucket extends InternalRange.Bucket { public Bucket( String key, double from, - double originalFrom, double to, - double originalTo, long docCount, List aggregations, boolean keyed, DocValueFormat formatter ) { - super(key, from, originalFrom, to, originalTo, docCount, InternalAggregations.from(aggregations), keyed, formatter); + super(key, from, to, docCount, InternalAggregations.from(aggregations), keyed, formatter); } public Bucket( String key, double from, - double originalFrom, double to, - double originalTo, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat formatter ) { - super(key, from, originalFrom, to, originalTo, docCount, aggregations, keyed, formatter); + super(key, from, to, docCount, aggregations, keyed, formatter); } @Override @@ -75,14 +71,6 @@ private Double internalGetTo() { return to; } - private Double internalGetOriginalFrom() { - return originalFrom; - } - - private Double internalGetOriginalTo() { - return originalTo; - } - @Override protected InternalRange.Factory getFactory() { return FACTORY; @@ -124,15 +112,13 @@ public InternalDateRange create(List ranges, InternalDateRange prototype public Bucket createBucket( String key, double from, - double originalFrom, double to, - double originalTo, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat formatter ) { - return new Bucket(key, from, originalFrom, to, originalTo, docCount, aggregations, keyed, formatter); + return new Bucket(key, from, to, docCount, aggregations, keyed, formatter); } @Override @@ -140,9 +126,7 @@ public Bucket createBucket(InternalAggregations aggregations, Bucket prototype) return new Bucket( prototype.getKey(), prototype.internalGetFrom(), - prototype.internalGetOriginalFrom(), prototype.internalGetTo(), - prototype.internalGetOriginalTo(), prototype.getDocCount(), aggregations, prototype.getKeyed(), diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistance.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistance.java index 9d5c4e8a07e3..028fce1b4c56 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistance.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistance.java @@ -23,17 +23,8 @@ public class InternalGeoDistance extends InternalRange ranges, InternalGeoDistance proto public Bucket createBucket( String key, double from, - double originalFrom, double to, - double originalTo, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat format ) { - return new Bucket(key, from, originalFrom, to, originalTo, docCount, aggregations, keyed); + return new Bucket(key, from, to, docCount, aggregations, keyed); } @Override @@ -93,9 +82,7 @@ public Bucket createBucket(InternalAggregations aggregations, Bucket prototype) return new Bucket( prototype.getKey(), ((Number) prototype.getFrom()).doubleValue(), - ((Number) prototype.getOriginalFrom()).doubleValue(), ((Number) prototype.getTo()).doubleValue(), - ((Number) prototype.getOriginalTo()).doubleValue(), prototype.getDocCount(), aggregations, prototype.getKeyed() diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java index d5b717446b64..1bdc142423d4 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalRange.java @@ -37,9 +37,7 @@ public static class Bucket extends InternalMultiBucketAggregation.InternalBucket protected final transient boolean keyed; protected final transient DocValueFormat format; protected final double from; - protected final double originalFrom; protected final double to; - protected final double originalTo; private final long docCount; private final InternalAggregations aggregations; private final String key; @@ -47,9 +45,7 @@ public static class Bucket extends InternalMultiBucketAggregation.InternalBucket public Bucket( String key, double from, - double originalFrom, double to, - double originalTo, long docCount, InternalAggregations aggregations, boolean keyed, @@ -57,11 +53,9 @@ public Bucket( ) { this.keyed = keyed; this.format = format; - this.key = key != null ? key : generateKey(originalFrom, originalTo, format); + this.key = key; this.from = from; - this.originalFrom = originalFrom; this.to = to; - this.originalTo = originalTo; this.docCount = docCount; this.aggregations = aggregations; } @@ -73,7 +67,7 @@ public String getKey() { @Override public String getKeyAsString() { - return key; + return this.key == null ? generateKey(this.from, this.to, this.format) : this.key; } @Override @@ -81,19 +75,11 @@ public Object getFrom() { return from; } - public Double getOriginalFrom() { - return originalFrom; - } - @Override public Object getTo() { return to; } - public Double getOriginalTo() { - return originalTo; - } - public boolean getKeyed() { return keyed; } @@ -104,19 +90,19 @@ public DocValueFormat getFormat() { @Override public String getFromAsString() { - if (Double.isInfinite(originalFrom)) { + if (Double.isInfinite(from)) { return null; } else { - return format.format(originalFrom).toString(); + return format.format(from).toString(); } } @Override public String getToAsString() { - if (Double.isInfinite(originalTo)) { + if (Double.isInfinite(to)) { return null; } else { - return format.format(originalTo).toString(); + return format.format(to).toString(); } } @@ -137,22 +123,23 @@ public InternalAggregations getAggregations() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + final String key = getKeyAsString(); if (keyed) { builder.startObject(key); } else { builder.startObject(); builder.field(CommonFields.KEY.getPreferredName(), key); } - if (Double.isInfinite(originalFrom) == false) { - builder.field(CommonFields.FROM.getPreferredName(), originalFrom); + if (Double.isInfinite(from) == false) { + builder.field(CommonFields.FROM.getPreferredName(), from); if (format != DocValueFormat.RAW) { - builder.field(CommonFields.FROM_AS_STRING.getPreferredName(), format.format(originalFrom)); + builder.field(CommonFields.FROM_AS_STRING.getPreferredName(), format.format(from)); } } - if (Double.isInfinite(originalTo) == false) { - builder.field(CommonFields.TO.getPreferredName(), originalTo); + if (Double.isInfinite(to) == false) { + builder.field(CommonFields.TO.getPreferredName(), to); if (format != DocValueFormat.RAW) { - builder.field(CommonFields.TO_AS_STRING.getPreferredName(), format.format(originalTo)); + builder.field(CommonFields.TO_AS_STRING.getPreferredName(), format.format(to)); } } builder.field(CommonFields.DOC_COUNT.getPreferredName(), docCount); @@ -170,14 +157,18 @@ private static String generateKey(double from, double to, DocValueFormat format) @Override public void writeTo(StreamOutput out) throws IOException { - out.writeString(key); + if (out.getVersion().onOrAfter(Version.V_8_1_0)) { + out.writeOptionalString(key); + } else { + out.writeString(key == null ? generateKey(from, to, format) : key); + } out.writeDouble(from); if (out.getVersion().onOrAfter(Version.V_7_17_0)) { - out.writeOptionalDouble(originalFrom); + out.writeOptionalDouble(from); } out.writeDouble(to); if (out.getVersion().onOrAfter(Version.V_7_17_0)) { - out.writeOptionalDouble(originalTo); + out.writeOptionalDouble(to); } out.writeVLong(docCount); aggregations.writeTo(out); @@ -223,15 +214,13 @@ public R create(String name, List ranges, DocValueFormat format, boolean keye public B createBucket( String key, double from, - double originalFrom, double to, - double originalTo, long docCount, InternalAggregations aggregations, boolean keyed, DocValueFormat format ) { - return (B) new Bucket(key, from, originalFrom, to, originalTo, docCount, aggregations, keyed, format); + return (B) new Bucket(key, from, to, docCount, aggregations, keyed, format); } @SuppressWarnings("unchecked") @@ -244,9 +233,7 @@ public B createBucket(InternalAggregations aggregations, B prototype) { return (B) new Bucket( prototype.getKey(), prototype.from, - prototype.originalFrom, prototype.to, - prototype.originalTo, prototype.getDocCount(), aggregations, prototype.keyed, @@ -276,25 +263,18 @@ public InternalRange(StreamInput in) throws IOException { int size = in.readVInt(); List ranges = new ArrayList<>(size); for (int i = 0; i < size; i++) { - String key = in.readString(); + String key = in.getVersion().onOrAfter(Version.V_8_1_0) ? in.readOptionalString() : in.readString(); double from = in.readDouble(); - Double originalFrom = in.getVersion().onOrAfter(Version.V_7_17_0) ? in.readOptionalDouble() : Double.valueOf(from); + if (in.getVersion().onOrAfter(Version.V_7_17_0)) { + in.readOptionalDouble(); + } double to = in.readDouble(); - Double originalTo = in.getVersion().onOrAfter(Version.V_7_17_0) ? in.readOptionalDouble() : Double.valueOf(to); + if (in.getVersion().onOrAfter(Version.V_7_17_0)) { + in.readOptionalDouble(); + } long docCount = in.readVLong(); - ranges.add( - getFactory().createBucket( - key, - from, - originalFrom, - to, - originalTo, - docCount, - InternalAggregations.readFrom(in), - keyed, - format - ) - ); + InternalAggregations aggregations = InternalAggregations.readFrom(in); + ranges.add(getFactory().createBucket(key, from, to, docCount, aggregations, keyed, format)); } this.ranges = ranges; } @@ -370,17 +350,7 @@ protected B reduceBucket(List buckets, AggregationReduceContext context) { } final InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context); Bucket prototype = buckets.get(0); - return getFactory().createBucket( - prototype.key, - prototype.from, - prototype.originalFrom, - prototype.to, - prototype.originalTo, - docCount, - aggs, - keyed, - format - ); + return getFactory().createBucket(prototype.key, prototype.from, prototype.to, docCount, aggs, keyed, format); } @Override diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java index 72103e1b8fef..34b06c372709 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java @@ -163,18 +163,9 @@ protected RangeAggregatorFactory innerBuild( Range[] ranges = processRanges(range -> { DocValueFormat parser = config.format(); assert parser != null; - Double from = fixPrecision.applyAsDouble(range.from); - Double to = fixPrecision.applyAsDouble(range.to); - if (range.fromAsStr != null) { - from = parser.parseDouble(range.fromAsStr, false, context::nowInMillis); - } - if (range.toAsStr != null) { - to = parser.parseDouble(range.toAsStr, false, context::nowInMillis); - } - double originalFrom = range.fromAsStr != null ? from : range.from; - double originalTo = range.toAsStr != null ? to : range.to; - String key = range.key != null ? range.key : generateKey(originalFrom, originalTo, config.format()); - return new Range(key, from, originalFrom, range.fromAsStr, to, originalTo, range.toAsStr); + double from = range.fromAsStr != null ? parser.parseDouble(range.fromAsStr, false, context::nowInMillis) : range.from; + double to = range.toAsStr != null ? parser.parseDouble(range.toAsStr, false, context::nowInMillis) : range.to; + return new Range(range.key, from, range.fromAsStr, to, range.toAsStr, fixPrecision); }); if (ranges.length == 0) { throw new IllegalArgumentException("No [ranges] specified for the [" + this.getName() + "] aggregation"); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregator.java index 0c5879392565..733481421112 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregator.java @@ -53,6 +53,7 @@ import java.util.Map; import java.util.Objects; import java.util.function.BiConsumer; +import java.util.function.DoubleUnaryOperator; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; @@ -90,6 +91,8 @@ public abstract class RangeAggregator extends BucketsAggregator { public static final ParseField RANGES_FIELD = new ParseField("ranges"); public static final ParseField KEYED_FIELD = new ParseField("keyed"); + static final DoubleUnaryOperator IDENTITY = DoubleUnaryOperator.identity(); + public static class Range implements Writeable, ToXContentObject { public static final ParseField KEY_FIELD = new ParseField("key"); public static final ParseField FROM_FIELD = new ParseField("from"); @@ -121,22 +124,33 @@ public static class Range implements Writeable, ToXContentObject { * AggregationContext, ValuesSourceConfig, AggregatorFactory, AggregatorFactories.Builder * )}) */ - public Range(String key, Double from, Double originalFrom, String fromAsStr, Double to, Double originalTo, String toAsStr) { + public Range( + final String key, + final Double from, + final String fromAsStr, + final Double to, + final String toAsStr, + final DoubleUnaryOperator fixPrecision + ) { this.key = key; - this.from = from == null ? Double.NEGATIVE_INFINITY : from; - this.originalFrom = originalFrom == null ? Double.NEGATIVE_INFINITY : originalFrom; + this.from = from == null ? Double.NEGATIVE_INFINITY : fixPrecision.applyAsDouble(from); + this.originalFrom = from == null ? Double.NEGATIVE_INFINITY : from; this.fromAsStr = fromAsStr; - this.to = to == null ? Double.POSITIVE_INFINITY : to; - this.originalTo = originalTo == null ? Double.POSITIVE_INFINITY : originalTo; + this.to = to == null ? Double.POSITIVE_INFINITY : fixPrecision.applyAsDouble(to); + this.originalTo = to == null ? Double.POSITIVE_INFINITY : to; this.toAsStr = toAsStr; } + public Range(String key, Double from, String fromAsStr, Double to, String toAsStr) { + this(key, from, fromAsStr, to, toAsStr, IDENTITY); + } + public Range(String key, Double from, Double to) { - this(key, from, from, null, to, to, null); + this(key, from, null, to, null); } public Range(String key, String from, String to) { - this(key, null, null, from, null, null, to); + this(key, null, from, null, to); } /** @@ -166,11 +180,11 @@ public void writeTo(StreamOutput out) throws IOException { } public double getFrom() { - return this.from; + return this.originalFrom; } public double getTo() { - return this.to; + return this.originalTo; } public Double getOriginalFrom() { @@ -232,7 +246,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws Double toDouble = to instanceof Number ? ((Number) to).doubleValue() : null; String fromStr = from instanceof String ? (String) from : null; String toStr = to instanceof String ? (String) to : null; - return new Range(key, fromDouble, fromDouble, fromStr, toDouble, toDouble, toStr); + return new Range(key, fromDouble, fromStr, toDouble, toStr); }); static { @@ -512,9 +526,7 @@ public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws I Range range = ranges[offsetInOwningOrd]; return rangeFactory.createBucket( range.key, - range.from, range.originalFrom, - range.to, range.originalTo, docCount, subAggregationResults, @@ -535,9 +547,7 @@ public InternalAggregation buildEmptyAggregation() { Range range = ranges[i]; org.elasticsearch.search.aggregations.bucket.range.Range.Bucket bucket = rangeFactory.createBucket( range.key, - range.from, range.originalFrom, - range.to, range.originalTo, 0, subAggs, @@ -589,9 +599,7 @@ public InternalAggregation buildEmptyAggregation() { InternalAggregations subAggs = buildEmptySubAggregations(); List buckets = new ArrayList<>(ranges.length); for (RangeAggregator.Range range : ranges) { - buckets.add( - factory.createBucket(range.key, range.from, range.originalFrom, range.to, range.originalTo, 0, subAggs, keyed, format) - ); + buckets.add(factory.createBucket(range.key, range.originalFrom, range.originalTo, 0, subAggs, keyed, format)); } return factory.create(name, buckets, format, keyed, metadata()); } @@ -829,17 +837,7 @@ protected InternalAggregation adapt(InternalAggregation delegateResult) { Range r = ranges[i]; InternalFilters.InternalBucket b = filters.getBuckets().get(i); buckets.add( - rangeFactory.createBucket( - r.getKey(), - r.getFrom(), - r.getOriginalFrom(), - r.getTo(), - r.getOriginalTo(), - b.getDocCount(), - b.getAggregations(), - keyed, - format - ) + rangeFactory.createBucket(r.getKey(), r.originalFrom, r.originalTo, b.getDocCount(), b.getAggregations(), keyed, format) ); } return rangeFactory.create(name(), buckets, format, keyed, filters.getMetadata()); diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java index 44490ad0a5f9..7bd246146fc0 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchResponseMergerTests.java @@ -472,8 +472,6 @@ public void testMergeAggs() throws InterruptedException { InternalDateRange.Bucket bucket = factory.createBucket( "bucket", 0D, - 0D, - 10000D, 10000D, count, InternalAggregations.EMPTY, diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRangeTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRangeTests.java index f81f867c6f88..e7b9513c2d9f 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRangeTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalDateRangeTests.java @@ -81,7 +81,7 @@ protected InternalDateRange createTestInstance( int docCount = randomIntBetween(0, 1000); double from = range.v1(); double to = range.v2(); - buckets.add(new InternalDateRange.Bucket("range_" + i, from, from, to, to, docCount, aggregations, keyed, format)); + buckets.add(new InternalDateRange.Bucket("range_" + i, from, to, docCount, aggregations, keyed, format)); } return new InternalDateRange(name, buckets, format, keyed, metadata); } @@ -116,17 +116,7 @@ protected InternalDateRange mutateInstance(InternalDateRange instance) { double from = randomDouble(); double to = from + randomDouble(); buckets.add( - new InternalDateRange.Bucket( - "range_a", - from, - from, - to, - to, - randomNonNegativeLong(), - InternalAggregations.EMPTY, - false, - format - ) + new InternalDateRange.Bucket("range_a", from, to, randomNonNegativeLong(), InternalAggregations.EMPTY, false, format) ); } case 3 -> { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistanceTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistanceTests.java index cb9d785b260e..3194e343d208 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistanceTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalGeoDistanceTests.java @@ -63,7 +63,7 @@ protected InternalGeoDistance createTestInstance( int docCount = randomIntBetween(0, 1000); double from = range.v1(); double to = range.v2(); - buckets.add(new InternalGeoDistance.Bucket("range_" + i, from, from, to, to, docCount, aggregations, keyed)); + buckets.add(new InternalGeoDistance.Bucket("range_" + i, from, to, docCount, aggregations, keyed)); } return new InternalGeoDistance(name, buckets, keyed, metadata); } @@ -97,16 +97,7 @@ protected InternalGeoDistance mutateInstance(InternalGeoDistance instance) { double from = randomDouble(); double to = from + randomDouble(); buckets.add( - new InternalGeoDistance.Bucket( - "range_a", - from, - from, - to, - to, - randomNonNegativeLong(), - InternalAggregations.EMPTY, - false - ) + new InternalGeoDistance.Bucket("range_a", from, to, randomNonNegativeLong(), InternalAggregations.EMPTY, false) ); } case 3 -> { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalRangeTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalRangeTests.java index ebdb3a2acfc4..b4003f55693f 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalRangeTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/InternalRangeTests.java @@ -76,7 +76,7 @@ public void setUp() throws Exception { int docCount = randomIntBetween(0, 1000); double from = range.v1(); double to = range.v2(); - buckets.add(new InternalRange.Bucket("range_" + i, from, from, to, to, docCount, aggregations, keyed, format)); + buckets.add(new InternalRange.Bucket("range_" + i, from, to, docCount, aggregations, keyed, format)); } return new InternalRange<>(name, buckets, format, keyed, metadata); } @@ -111,17 +111,7 @@ protected Class parsedRange double from = randomDouble(); double to = from + randomDouble(); buckets.add( - new InternalRange.Bucket( - "range_a", - from, - from, - to, - to, - randomNonNegativeLong(), - InternalAggregations.EMPTY, - false, - format - ) + new InternalRange.Bucket("range_a", from, to, randomNonNegativeLong(), InternalAggregations.EMPTY, false, format) ); } case 3 -> { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java index 52638e4267ff..45bba97f0e54 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java @@ -121,7 +121,7 @@ public void testDoubleRangesExclusiveEndpoint() throws IOException { ); } - public void testFloatRangeKeys() throws IOException { + public void testFloatRangeFromAndToValues() throws IOException { final String fieldName = "test"; MappedFieldType field = new NumberFieldMapper.NumberFieldType(fieldName, NumberType.FLOAT); testCase( @@ -138,15 +138,14 @@ public void testFloatRangeKeys() throws IOException { assertEquals(2, ranges.size()); assertEquals("5.0-6.0", ranges.get(0).getKeyAsString()); assertEquals("6.0-10.6", ranges.get(1).getKeyAsString()); + assertEquals(5.0D, ranges.get(0).getFrom()); + assertEquals(6.0D, ranges.get(0).getTo()); + assertEquals(6.0D, ranges.get(1).getFrom()); + assertEquals(10.6D, ranges.get(1).getTo()); assertEquals("5.0", ranges.get(0).getFromAsString()); assertEquals("6.0", ranges.get(0).getToAsString()); assertEquals("6.0", ranges.get(1).getFromAsString()); assertEquals("10.6", ranges.get(1).getToAsString()); - // NOTE: `getOriginalFrom` and `getOriginalTo` return double - assertEquals(5.0D, ranges.get(0).getOriginalFrom(), 0.0000000000001); - assertEquals(6.0D, ranges.get(0).getOriginalTo(), 0.0000000000001); - assertEquals(6.0D, ranges.get(1).getOriginalFrom(), 0.0000000000001); - assertEquals(10.6D, ranges.get(1).getOriginalTo(), 0.0000000000001); assertEquals(1, ranges.get(0).getDocCount()); assertEquals(2, ranges.get(1).getDocCount()); }, @@ -154,7 +153,7 @@ public void testFloatRangeKeys() throws IOException { ); } - public void testDoubleRangeKeys() throws IOException { + public void testDoubleRangeFromAndToValues() throws IOException { final String fieldName = "test"; MappedFieldType field = new NumberFieldMapper.NumberFieldType(fieldName, NumberType.DOUBLE); testCase( @@ -171,14 +170,14 @@ public void testDoubleRangeKeys() throws IOException { assertEquals(2, ranges.size()); assertEquals("5.0-6.0", ranges.get(0).getKeyAsString()); assertEquals("6.0-10.6", ranges.get(1).getKeyAsString()); + assertEquals(5.0D, ranges.get(0).getFrom()); + assertEquals(6.0D, ranges.get(0).getTo()); + assertEquals(6.0D, ranges.get(1).getFrom()); + assertEquals(10.6D, ranges.get(1).getTo()); assertEquals("5.0", ranges.get(0).getFromAsString()); assertEquals("6.0", ranges.get(0).getToAsString()); assertEquals("6.0", ranges.get(1).getFromAsString()); assertEquals("10.6", ranges.get(1).getToAsString()); - assertEquals(5.0D, ranges.get(0).getOriginalFrom(), 0.0000000000001); - assertEquals(6.0D, ranges.get(0).getOriginalTo(), 0.0000000000001); - assertEquals(6.0D, ranges.get(1).getOriginalFrom(), 0.0000000000001); - assertEquals(10.6D, ranges.get(1).getOriginalTo(), 0.0000000000001); assertEquals(1, ranges.get(0).getDocCount()); assertEquals(2, ranges.get(1).getDocCount()); }, @@ -574,9 +573,11 @@ public void execute() { } }, (InternalRange r, Class impl, Map> debug) -> { assertThat( - r.getBuckets().stream().map(InternalRange.Bucket::getKey).collect(toList()), + r.getBuckets().stream().map(InternalRange.Bucket::getKeyAsString).collect(toList()), equalTo(List.of("0.0-1.0", "1.0-2.0", "2.0-3.0")) ); + assertThat(r.getBuckets().stream().map(InternalRange.Bucket::getFrom).collect(toList()), equalTo(List.of(0.0, 1.0, 2.0))); + assertThat(r.getBuckets().stream().map(InternalRange.Bucket::getTo).collect(toList()), equalTo(List.of(1.0, 2.0, 3.0))); assertThat( r.getBuckets().stream().map(InternalRange.Bucket::getDocCount).collect(toList()), equalTo(List.of(totalDocs, 0L, 0L)) @@ -610,9 +611,11 @@ public void execute() { }, (InternalRange r, Class impl, Map> debug) -> { assertThat( - r.getBuckets().stream().map(InternalRange.Bucket::getKey).collect(toList()), + r.getBuckets().stream().map(InternalRange.Bucket::getKeyAsString).collect(toList()), equalTo(List.of("0.0-1.0", "1.0-2.0", "2.0-3.0")) ); + assertThat(r.getBuckets().stream().map(InternalRange.Bucket::getFrom).collect(toList()), equalTo(List.of(0.0, 1.0, 2.0))); + assertThat(r.getBuckets().stream().map(InternalRange.Bucket::getTo).collect(toList()), equalTo(List.of(1.0, 2.0, 3.0))); assertThat( r.getBuckets().stream().map(InternalRange.Bucket::getDocCount).collect(toList()), equalTo(List.of(totalDocs, 0L, 0L))