Skip to content

Commit 69b1463

Browse files
authored
Add unit tests for BinaryRangeAggregator/InternalBinaryRange (#23255)
* Add unit tests for BinaryRangeAggregator/InternalBinaryRange Relates #22278
1 parent 872412f commit 69b1463

File tree

5 files changed

+263
-11
lines changed

5 files changed

+263
-11
lines changed

core/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public Mapper.Builder<?,?> parse(String name, Map<String, Object> node, ParserCo
124124

125125
public static final class IpFieldType extends MappedFieldType {
126126

127-
IpFieldType() {
127+
public IpFieldType() {
128128
super();
129129
setTokenized(false);
130130
setHasDocValues(true);

core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/BinaryRangeAggregator.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
*/
1919
package org.elasticsearch.search.aggregations.bucket.range;
2020

21-
import java.io.IOException;
22-
import java.util.ArrayList;
23-
import java.util.Arrays;
24-
import java.util.Comparator;
25-
import java.util.List;
26-
import java.util.Map;
27-
28-
import static java.util.Collections.emptyList;
29-
3021
import org.apache.lucene.index.LeafReaderContext;
3122
import org.apache.lucene.index.SortedSetDocValues;
3223
import org.apache.lucene.util.BytesRef;
@@ -42,6 +33,15 @@
4233
import org.elasticsearch.search.aggregations.support.ValuesSource;
4334
import org.elasticsearch.search.internal.SearchContext;
4435

36+
import java.io.IOException;
37+
import java.util.ArrayList;
38+
import java.util.Arrays;
39+
import java.util.Comparator;
40+
import java.util.List;
41+
import java.util.Map;
42+
43+
import static java.util.Collections.emptyList;
44+
4545
/** A range aggregator for values that are stored in SORTED_SET doc values. */
4646
public final class BinaryRangeAggregator extends BucketsAggregator {
4747

core/src/main/java/org/elasticsearch/search/aggregations/bucket/range/InternalBinaryRange.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
1920
package org.elasticsearch.search.aggregations.bucket.range;
2021

2122
import org.apache.lucene.util.BytesRef;
@@ -35,6 +36,7 @@
3536
import java.util.Arrays;
3637
import java.util.List;
3738
import java.util.Map;
39+
import java.util.Objects;
3840

3941
import static java.util.Collections.unmodifiableList;
4042

@@ -166,6 +168,25 @@ public String getToAsString() {
166168
return to == null ? null : format.format(to);
167169
}
168170

171+
@Override
172+
public boolean equals(Object o) {
173+
if (this == o) return true;
174+
if (o == null || getClass() != o.getClass()) return false;
175+
176+
Bucket bucket = (Bucket) o;
177+
178+
if (docCount != bucket.docCount) return false;
179+
// keyed and format are ignored since they are already tested on the InternalBinaryRange object
180+
return Objects.equals(key, bucket.key) &&
181+
Objects.equals(from, bucket.from) &&
182+
Objects.equals(to, bucket.to) &&
183+
Objects.equals(aggregations, bucket.aggregations);
184+
}
185+
186+
@Override
187+
public int hashCode() {
188+
return Objects.hash(getClass(), docCount, key, from, to, aggregations);
189+
}
169190
}
170191

171192
private final DocValueFormat format;
@@ -263,4 +284,17 @@ public XContentBuilder doXContentBody(XContentBuilder builder,
263284
}
264285
return builder;
265286
}
266-
}
287+
288+
@Override
289+
public boolean doEquals(Object obj) {
290+
InternalBinaryRange that = (InternalBinaryRange) obj;
291+
return Objects.equals(buckets, that.buckets)
292+
&& Objects.equals(format, that.format)
293+
&& Objects.equals(keyed, that.keyed);
294+
}
295+
296+
@Override
297+
public int doHashCode() {
298+
return Objects.hash(buckets, format, keyed);
299+
}
300+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.util.BytesRef;
22+
import org.elasticsearch.common.collect.Tuple;
23+
import org.elasticsearch.common.io.stream.Writeable;
24+
import org.elasticsearch.search.DocValueFormat;
25+
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
26+
import org.elasticsearch.search.aggregations.InternalAggregations;
27+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
28+
import org.junit.Before;
29+
30+
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import java.util.Collections;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
public class InternalBinaryRangeTests extends InternalAggregationTestCase<InternalBinaryRange> {
37+
private Tuple<BytesRef, BytesRef>[] RANGES;
38+
39+
@Before
40+
public void randomSortedRanges() {
41+
int numRanges = randomIntBetween(1, 10);
42+
Tuple<BytesRef, BytesRef>[] ranges = new Tuple[numRanges];
43+
for (int i = 0; i < numRanges; i++) {
44+
BytesRef[] values = new BytesRef[2];
45+
values[0] = new BytesRef(randomAsciiOfLength(15));
46+
values[1] = new BytesRef(randomAsciiOfLength(15));
47+
Arrays.sort(values);
48+
ranges[i] = new Tuple(values[0], values[1]);
49+
}
50+
Arrays.sort(ranges, (t1, t2) -> t1.v1().compareTo(t2.v1()));
51+
RANGES = ranges;
52+
}
53+
54+
55+
@Override
56+
protected InternalBinaryRange createTestInstance(String name, List<PipelineAggregator> pipelineAggregators,
57+
Map<String, Object> metaData) {
58+
boolean keyed = randomBoolean();
59+
DocValueFormat format = DocValueFormat.RAW;
60+
List<InternalBinaryRange.Bucket> buckets = new ArrayList<>();
61+
for (int i = 0; i < RANGES.length; ++i) {
62+
final int docCount = randomIntBetween(1, 100);
63+
buckets.add(new InternalBinaryRange.Bucket(format, keyed, randomAsciiOfLength(10),
64+
RANGES[i].v1(), RANGES[i].v2(), docCount, InternalAggregations.EMPTY));
65+
}
66+
return new InternalBinaryRange(name, format, keyed, buckets, pipelineAggregators, Collections.emptyMap());
67+
}
68+
69+
@Override
70+
protected Writeable.Reader<InternalBinaryRange> instanceReader() {
71+
return InternalBinaryRange::new;
72+
}
73+
74+
@Override
75+
protected void assertReduced(InternalBinaryRange reduced, List<InternalBinaryRange> inputs) {
76+
int pos = 0;
77+
for (InternalBinaryRange input : inputs) {
78+
assertEquals(reduced.getBuckets().size(), input.getBuckets().size());
79+
}
80+
for (Range.Bucket bucket : reduced.getBuckets()) {
81+
int expectedCount = 0;
82+
for (InternalBinaryRange input : inputs) {
83+
expectedCount += input.getBuckets().get(pos).getDocCount();
84+
}
85+
assertEquals(expectedCount, bucket.getDocCount());
86+
pos ++;
87+
}
88+
}
89+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)