|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.search.aggregations.bucket.range; |
| 20 | + |
| 21 | +import org.apache.lucene.document.Document; |
| 22 | +import org.apache.lucene.document.InetAddressPoint; |
| 23 | +import org.apache.lucene.document.SortedSetDocValuesField; |
| 24 | +import org.apache.lucene.index.IndexReader; |
| 25 | +import org.apache.lucene.index.RandomIndexWriter; |
| 26 | +import org.apache.lucene.search.IndexSearcher; |
| 27 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 28 | +import org.apache.lucene.store.Directory; |
| 29 | +import org.apache.lucene.util.BytesRef; |
| 30 | +import org.elasticsearch.common.collect.Tuple; |
| 31 | +import org.elasticsearch.common.network.NetworkAddress; |
| 32 | +import org.elasticsearch.index.mapper.IpFieldMapper; |
| 33 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 34 | +import org.elasticsearch.search.DocValueFormat; |
| 35 | +import org.elasticsearch.search.aggregations.AggregatorTestCase; |
| 36 | +import org.elasticsearch.search.aggregations.bucket.range.ip.IpRangeAggregationBuilder; |
| 37 | + |
| 38 | +import java.net.InetAddress; |
| 39 | +import java.net.UnknownHostException; |
| 40 | +import java.util.Arrays; |
| 41 | + |
| 42 | +public class IpRangeAggregatorTests extends AggregatorTestCase { |
| 43 | + |
| 44 | + private static InetAddress randomIp(boolean v4, boolean withNull) { |
| 45 | + if (withNull && rarely()) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + try { |
| 49 | + if (v4) { |
| 50 | + byte[] ipv4 = new byte[4]; |
| 51 | + random().nextBytes(ipv4); |
| 52 | + return InetAddress.getByAddress(ipv4); |
| 53 | + } else { |
| 54 | + byte[] ipv6 = new byte[16]; |
| 55 | + random().nextBytes(ipv6); |
| 56 | + return InetAddress.getByAddress(ipv6); |
| 57 | + } |
| 58 | + } catch (UnknownHostException e) { |
| 59 | + throw new AssertionError(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static boolean isInRange(BytesRef value, BytesRef from, BytesRef to) { |
| 64 | + if (to == null || to.compareTo(value) > 0 && (from == null || from.compareTo(value) <= 0)) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + public void testRanges() throws Exception { |
| 71 | + boolean v4 = randomBoolean(); |
| 72 | + IpRangeAggregationBuilder builder = new IpRangeAggregationBuilder("test_agg").field("field"); |
| 73 | + int numRanges = randomIntBetween(1, 10); |
| 74 | + Tuple<BytesRef, BytesRef>[] requestedRanges = new Tuple[numRanges]; |
| 75 | + for (int i = 0; i < numRanges; i++) { |
| 76 | + Tuple<InetAddress, BytesRef>[] arr = new Tuple[2]; |
| 77 | + for (int j = 0; j < 2; j++) { |
| 78 | + InetAddress addr = randomIp(v4, true); |
| 79 | + if (addr == null) { |
| 80 | + arr[j] = new Tuple(null, null); |
| 81 | + } else { |
| 82 | + arr[j] = new Tuple(addr, new BytesRef(InetAddressPoint.encode(addr))); |
| 83 | + } |
| 84 | + } |
| 85 | + Arrays.sort(arr, (t1, t2) -> t1.v2().compareTo(t2.v2())); |
| 86 | + builder.addRange(NetworkAddress.format(arr[0].v1()), NetworkAddress.format(arr[1].v1())); |
| 87 | + requestedRanges[i] = new Tuple(arr[0].v2(), arr[1].v2()); |
| 88 | + } |
| 89 | + Arrays.sort(requestedRanges, (t1, t2) -> t1.v1().compareTo(t2.v1())); |
| 90 | + int[] expectedCounts = new int[numRanges]; |
| 91 | + try (Directory dir = newDirectory(); |
| 92 | + RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { |
| 93 | + int numDocs = randomIntBetween(10, 100); |
| 94 | + for (int i = 0; i < numDocs; i++) { |
| 95 | + Document doc = new Document(); |
| 96 | + int numValues = randomIntBetween(1, 5); |
| 97 | + BytesRef[] values = new BytesRef[numValues]; |
| 98 | + for (int j = 0; j < numValues; j++) { |
| 99 | + values[j] = new BytesRef(InetAddressPoint.encode(randomIp(v4, false))); |
| 100 | + doc.add(new SortedSetDocValuesField("field", values[j])); |
| 101 | + } |
| 102 | + Arrays.sort(values); |
| 103 | + for (int j = 0; j < numRanges; j++) { |
| 104 | + for (int k = 0; k < numValues; k++) { |
| 105 | + if (isInRange(values[k], requestedRanges[j].v1(), requestedRanges[j].v2())) { |
| 106 | + expectedCounts[j]++; |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + w.addDocument(doc); |
| 112 | + } |
| 113 | + MappedFieldType fieldType = new IpFieldMapper.IpFieldType(); |
| 114 | + fieldType.setName("field"); |
| 115 | + try (IndexReader reader = w.getReader()) { |
| 116 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 117 | + InternalBinaryRange range = search(searcher, new MatchAllDocsQuery(), builder, fieldType); |
| 118 | + assertEquals(numRanges, range.getBuckets().size()); |
| 119 | + for (int i = 0; i < range.getBuckets().size(); i++) { |
| 120 | + Tuple<BytesRef, BytesRef> expected = requestedRanges[i]; |
| 121 | + Range.Bucket bucket = range.getBuckets().get(i); |
| 122 | + assertEquals(DocValueFormat.IP.format(expected.v1()), bucket.getFrom()); |
| 123 | + assertEquals(DocValueFormat.IP.format(expected.v2()), bucket.getTo()); |
| 124 | + assertEquals(expectedCounts[i], bucket.getDocCount()); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments