diff options
author | Adrien Grand <jpountz@gmail.com> | 2017-02-17 18:01:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-17 18:01:40 +0100 |
commit | 3bd1d46fc7ff49d08ccf6b0ef91acb028cc5d8e2 (patch) | |
tree | a02afd23f89d8f2f840ca48f50e1332ccfd50ed6 /core/src/test/java/org/elasticsearch/search/aggregations/bucket | |
parent | 578853f264e8ddae5c919b1b7ca35524cb292d62 (diff) |
Add unit tests for terms aggregation objects. (#23149)
Relates #22278
Diffstat (limited to 'core/src/test/java/org/elasticsearch/search/aggregations/bucket')
4 files changed, 281 insertions, 0 deletions
diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTermsTests.java new file mode 100644 index 0000000000..757d5647a1 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTermsTests.java @@ -0,0 +1,66 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.bucket.terms; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.DocValueFormat; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class DoubleTermsTests extends InternalTermsTestCase { + + @Override + protected InternalTerms<?, ?> createTestInstance( + String name, + List<PipelineAggregator> pipelineAggregators, + Map<String, Object> metaData) { + Terms.Order order = Terms.Order.count(false); + long minDocCount = 1; + int requiredSize = 3; + int shardSize = requiredSize + 2; + DocValueFormat format = DocValueFormat.RAW; + boolean showTermDocCountError = false; + long docCountError = -1; + long otherDocCount = 0; + List<DoubleTerms.Bucket> buckets = new ArrayList<>(); + final int numBuckets = randomInt(shardSize); + Set<Double> terms = new HashSet<>(); + for (int i = 0; i < numBuckets; ++i) { + double term = randomValueOtherThanMany(d -> terms.add(d) == false, random()::nextDouble); + int docCount = randomIntBetween(1, 100); + buckets.add(new DoubleTerms.Bucket(term, docCount, InternalAggregations.EMPTY, + showTermDocCountError, docCountError, format)); + } + return new DoubleTerms(name, order, requiredSize, minDocCount, pipelineAggregators, + metaData, format, shardSize, showTermDocCountError, otherDocCount, buckets, docCountError); + } + + @Override + protected Reader<InternalTerms<?, ?>> instanceReader() { + return DoubleTerms::new; + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTermsTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTermsTestCase.java new file mode 100644 index 0000000000..03031633f7 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTermsTestCase.java @@ -0,0 +1,82 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.bucket.terms; + +import org.elasticsearch.search.aggregations.InternalAggregationTestCase; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Map.Entry; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public abstract class InternalTermsTestCase extends InternalAggregationTestCase<InternalTerms<?,?>> { + + @Override + protected InternalTerms<?, ?> createUnmappedInstance( + String name, + List<PipelineAggregator> pipelineAggregators, + Map<String, Object> metaData) { + InternalTerms<?, ?> testInstance = createTestInstance(name, pipelineAggregators, metaData); + return new UnmappedTerms(name, testInstance.order, testInstance.requiredSize, testInstance.minDocCount, + pipelineAggregators, metaData); + } + + @Override + protected void assertReduced(InternalTerms<?, ?> reduced, List<InternalTerms<?, ?>> inputs) { + final int requiredSize = inputs.get(0).requiredSize; + Map<Object, Long> reducedCounts = toCounts(reduced.getBuckets().stream()); + Map<Object, Long> totalCounts = toCounts(inputs.stream().map(Terms::getBuckets).flatMap(List::stream)); + + assertEquals(reducedCounts.size() == requiredSize, + totalCounts.size() >= requiredSize); + + Map<Object, Long> expectedReducedCounts = new HashMap<>(totalCounts); + expectedReducedCounts.keySet().retainAll(reducedCounts.keySet()); + assertEquals(expectedReducedCounts, reducedCounts); + + final long minFinalcount = reduced.getBuckets().isEmpty() + ? -1 + : reduced.getBuckets().get(reduced.getBuckets().size() - 1).getDocCount(); + Map<Object, Long> evictedTerms = new HashMap<>(totalCounts); + evictedTerms.keySet().removeAll(reducedCounts.keySet()); + Optional<Entry<Object, Long>> missingTerm = evictedTerms.entrySet().stream() + .filter(e -> e.getValue() > minFinalcount).findAny(); + if (missingTerm.isPresent()) { + fail("Missed term: " + missingTerm + " from " + reducedCounts); + } + + final long reducedTotalDocCount = reduced.getSumOfOtherDocCounts() + + reduced.getBuckets().stream().mapToLong(Terms.Bucket::getDocCount).sum(); + final long expectedTotalDocCount = inputs.stream().map(Terms::getBuckets) + .flatMap(List::stream).mapToLong(Terms.Bucket::getDocCount).sum(); + assertEquals(expectedTotalDocCount, reducedTotalDocCount); + } + + private static Map<Object, Long> toCounts(Stream<? extends Terms.Bucket> buckets) { + return buckets.collect(Collectors.toMap( + Terms.Bucket::getKey, + Terms.Bucket::getDocCount, + Long::sum)); + } +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsTests.java new file mode 100644 index 0000000000..ff95984bc3 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/LongTermsTests.java @@ -0,0 +1,66 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.bucket.terms; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.DocValueFormat; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class LongTermsTests extends InternalTermsTestCase { + + @Override + protected InternalTerms<?, ?> createTestInstance( + String name, + List<PipelineAggregator> pipelineAggregators, + Map<String, Object> metaData) { + Terms.Order order = Terms.Order.count(false); + long minDocCount = 1; + int requiredSize = 3; + int shardSize = requiredSize + 2; + DocValueFormat format = DocValueFormat.RAW; + boolean showTermDocCountError = false; + long docCountError = -1; + long otherDocCount = 0; + List<LongTerms.Bucket> buckets = new ArrayList<>(); + final int numBuckets = randomInt(shardSize); + Set<Long> terms = new HashSet<>(); + for (int i = 0; i < numBuckets; ++i) { + long term = randomValueOtherThanMany(l -> terms.add(l) == false, random()::nextLong); + int docCount = randomIntBetween(1, 100); + buckets.add(new LongTerms.Bucket(term, docCount, InternalAggregations.EMPTY, + showTermDocCountError, docCountError, format)); + } + return new LongTerms(name, order, requiredSize, minDocCount, pipelineAggregators, + metaData, format, shardSize, showTermDocCountError, otherDocCount, buckets, docCountError); + } + + @Override + protected Reader<InternalTerms<?, ?>> instanceReader() { + return LongTerms::new; + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/StringTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/StringTermsTests.java new file mode 100644 index 0000000000..d20f546a54 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/StringTermsTests.java @@ -0,0 +1,67 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.search.aggregations.bucket.terms; + +import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.DocValueFormat; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class StringTermsTests extends InternalTermsTestCase { + + @Override + protected InternalTerms<?, ?> createTestInstance( + String name, + List<PipelineAggregator> pipelineAggregators, + Map<String, Object> metaData) { + Terms.Order order = Terms.Order.count(false); + long minDocCount = 1; + int requiredSize = 3; + int shardSize = requiredSize + 2; + DocValueFormat format = DocValueFormat.RAW; + boolean showTermDocCountError = false; + long docCountError = -1; + long otherDocCount = 0; + List<StringTerms.Bucket> buckets = new ArrayList<>(); + final int numBuckets = randomInt(shardSize); + Set<BytesRef> terms = new HashSet<>(); + for (int i = 0; i < numBuckets; ++i) { + BytesRef term = randomValueOtherThanMany(b -> terms.add(b) == false, () -> new BytesRef(randomAsciiOfLength(10))); + int docCount = randomIntBetween(1, 100); + buckets.add(new StringTerms.Bucket(term, docCount, InternalAggregations.EMPTY, + showTermDocCountError, docCountError, format)); + } + return new StringTerms(name, order, requiredSize, minDocCount, pipelineAggregators, + metaData, format, shardSize, showTermDocCountError, otherDocCount, buckets, docCountError); + } + + @Override + protected Reader<InternalTerms<?, ?>> instanceReader() { + return StringTerms::new; + } + +} |