From 182e11969952fcc157d306e991214462d1a47ba2 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 8 Dec 2016 15:58:32 +0100 Subject: [PATCH] IP range masks exclude the maximum address of the range. (#22018) Closes #22005 --- .../range/ip/IpRangeAggregationBuilder.java | 15 +++++++- .../aggregations/bucket/IpRangeTests.java | 37 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ip/IpRangeAggregationBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ip/IpRangeAggregationBuilder.java index bd2353b5099..cc044ed6235 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ip/IpRangeAggregationBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ip/IpRangeAggregationBuilder.java @@ -55,6 +55,7 @@ public final class IpRangeAggregationBuilder private static final InternalAggregation.Type TYPE = new InternalAggregation.Type(NAME); public static class Range implements ToXContent { + private final String key; private final String from; private final String to; @@ -94,8 +95,18 @@ public final class IpRangeAggregationBuilder } this.key = key; try { - this.from = InetAddresses.toAddrString(InetAddress.getByAddress(lower)); - this.to = InetAddresses.toAddrString(InetAddress.getByAddress(upper)); + InetAddress fromAddress = InetAddress.getByAddress(lower); + if (fromAddress.equals(InetAddressPoint.MIN_VALUE)) { + this.from = null; + } else { + this.from = InetAddresses.toAddrString(fromAddress); + } + InetAddress inclusiveToAddress = InetAddress.getByAddress(upper); + if (inclusiveToAddress.equals(InetAddressPoint.MAX_VALUE)) { + this.to = null; + } else { + this.to = InetAddresses.toAddrString(InetAddressPoint.nextUp(inclusiveToAddress)); + } } catch (UnknownHostException bogus) { throw new AssertionError(bogus); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IpRangeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IpRangeTests.java index 5d86571f08f..4e9c1e00eab 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IpRangeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IpRangeTests.java @@ -87,4 +87,41 @@ public class IpRangeTests extends BaseAggregationTestCase