From b8c2f05ff54e8236a0b9eb76428e564d60884a23 Mon Sep 17 00:00:00 2001 From: Ruflin Date: Mon, 27 Jul 2015 09:11:04 +0200 Subject: [PATCH] Revert change to set longTo to "MAX_IP -1" and improve test suite to check for range --- .../bucket/range/ipv4/IPv4RangeBuilder.java | 3 +- .../aggregations/bucket/IPv4RangeTests.java | 72 ++++++++++--------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java index 86eb7eb2bcb..6d17bee5764 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/ipv4/IPv4RangeBuilder.java @@ -151,8 +151,9 @@ public class IPv4RangeBuilder extends AbstractRangeBuilder { int to = from + (~mask); long longTo = intIpToLongIp(to) + 1; // we have to +1 here as the range is non-inclusive on the "to" side + if (longTo == InternalIPv4Range.MAX_IP) { - longTo = InternalIPv4Range.MAX_IP - 1; + longTo = -1; } return new long[] { longFrom, longTo }; diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java index 244df1303ea..bf707a29972 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/IPv4RangeTests.java @@ -872,8 +872,8 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest { } @Test - public void cidr0Mask0() { - SearchResponse response = client().prepareSearch("idx_unmapped") + public void mask0() { + SearchResponse response = client().prepareSearch("idx") .addAggregation(ipRange("range") .field("ip") .addMaskRange("0.0.0.0/0")) @@ -881,7 +881,6 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest { assertSearchResponse(response); - Range range = response.getAggregations().get("range"); assertThat(range, notNullValue()); assertThat(range.getName(), equalTo("range")); @@ -890,53 +889,58 @@ public class IPv4RangeTests extends ElasticsearchIntegrationTest { Range.Bucket bucket = buckets.get(0); assertThat((String) bucket.getKey(), equalTo("0.0.0.0/0")); - assertThat(bucket.getFromAsString(), nullValue()); - assertThat(bucket.getToAsString(), equalTo("255.255.255.255")); + assertThat(bucket.getToAsString(), nullValue()); + assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY)); + assertEquals(255l, bucket.getDocCount()); } + @Test - public void cidr0Mask1() { - SearchResponse response = client().prepareSearch("idx_unmapped") + public void mask0SpecialIps() throws Exception { + assertAcked(prepareCreate("idx_range") + .addMapping("type", "ip", "type=ip", "ips", "type=ip")); + IndexRequestBuilder[] builders = new IndexRequestBuilder[4]; + + builders[0] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder() + .startObject() + .field("ip", "0.0.0.0") + .endObject()); + + builders[1] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder() + .startObject() + .field("ip", "0.0.0.255") + .endObject()); + + builders[2] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder() + .startObject() + .field("ip", "255.255.255.0") + .endObject()); + + builders[3] = client().prepareIndex("idx_range", "type").setSource(jsonBuilder() + .startObject() + .field("ip", "255.255.255.255") + .endObject()); + + indexRandom(true, builders); + ensureSearchable(); + + SearchResponse response = client().prepareSearch("idx_range") .addAggregation(ipRange("range") .field("ip") - .addMaskRange("0.0.0.0/1")) + .addMaskRange("0.0.0.0/0")) .execute().actionGet(); assertSearchResponse(response); - Range range = response.getAggregations().get("range"); + assertThat(range, notNullValue()); assertThat(range.getName(), equalTo("range")); List buckets = range.getBuckets(); assertThat(range.getBuckets().size(), equalTo(1)); Range.Bucket bucket = buckets.get(0); - assertThat((String) bucket.getKey(), equalTo("0.0.0.0/1")); - assertThat(bucket.getFromAsString(), nullValue()); - assertThat(bucket.getToAsString(), equalTo("128.0.0.0")); - } - - @Test - public void cidr0Mask2() { - SearchResponse response = client().prepareSearch("idx_unmapped") - .addAggregation(ipRange("range") - .field("ip") - .addMaskRange("0.0.0.0/2")) - .execute().actionGet(); - - assertSearchResponse(response); - - - Range range = response.getAggregations().get("range"); - assertThat(range, notNullValue()); - assertThat(range.getName(), equalTo("range")); - List buckets = range.getBuckets(); - assertThat(range.getBuckets().size(), equalTo(1)); - - Range.Bucket bucket = buckets.get(0); - assertThat(bucket.getFromAsString(), nullValue()); - assertThat(bucket.getToAsString(), equalTo("64.0.0.0")); + assertEquals(4l, bucket.getDocCount()); } }