diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml index 309fd865a22..fd2d0f1ba55 100644 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml @@ -437,18 +437,14 @@ - - - - diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregation.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregation.java index 2da4ae7fe33..7ce66e4ae44 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregation.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregation.java @@ -47,7 +47,8 @@ public abstract class InternalSingleBucketAggregation extends InternalAggregatio * @param docCount The document count in the single bucket. * @param aggregations The already built sub-aggregations that are associated with the bucket. */ - protected InternalSingleBucketAggregation(String name, long docCount, InternalAggregations aggregations, List pipelineAggregators, Map metaData) { + protected InternalSingleBucketAggregation(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { super(name, pipelineAggregators, metaData); this.docCount = docCount; this.aggregations = aggregations; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilter.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilter.java index 09f61142324..5ee89ee05ee 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilter.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilter.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.elasticsearch.search.aggregations.bucket.filter; import org.elasticsearch.common.io.stream.StreamInput; @@ -28,7 +29,8 @@ import java.util.List; import java.util.Map; public class InternalFilter extends InternalSingleBucketAggregation implements Filter { - InternalFilter(String name, long docCount, InternalAggregations subAggregations, List pipelineAggregators, Map metaData) { + InternalFilter(String name, long docCount, InternalAggregations subAggregations, List pipelineAggregators, + Map metaData) { super(name, docCount, subAggregations, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissing.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissing.java index 5a46e4ce95f..79bf95a2e8c 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissing.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissing.java @@ -28,7 +28,8 @@ import java.util.List; import java.util.Map; public class InternalMissing extends InternalSingleBucketAggregation implements Missing { - InternalMissing(String name, long docCount, InternalAggregations aggregations, List pipelineAggregators, Map metaData) { + InternalMissing(String name, long docCount, InternalAggregations aggregations, List pipelineAggregators, + Map metaData) { super(name, docCount, aggregations, pipelineAggregators, metaData); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNested.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNested.java index 4eedc6a3530..485a208a0ca 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNested.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNested.java @@ -31,8 +31,8 @@ import java.util.Map; * Result of the {@link ReverseNestedAggregator}. */ public class InternalReverseNested extends InternalSingleBucketAggregation implements ReverseNested { - public InternalReverseNested(String name, long docCount, InternalAggregations aggregations, List pipelineAggregators, - Map metaData) { + public InternalReverseNested(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { super(name, docCount, aggregations, pipelineAggregators, metaData); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java index 74d01ed201e..17694b8868d 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java @@ -50,11 +50,12 @@ public abstract class InternalAggregationTestCase } public final void testReduceRandom() { + String name = randomAsciiOfLength(5); List inputs = new ArrayList<>(); List toReduce = new ArrayList<>(); int toReduceSize = between(1, 200); for (int i = 0; i < toReduceSize; i++) { - T t = randomBoolean() ? createUnmappedInstance() : createTestInstance(); + T t = randomBoolean() ? createUnmappedInstance(name) : createTestInstance(name); inputs.add(t); toReduce.add(t); } @@ -87,7 +88,10 @@ public abstract class InternalAggregationTestCase @Override protected final T createTestInstance() { - String name = randomAsciiOfLength(5); + return createTestInstance(randomAsciiOfLength(5)); + } + + private T createTestInstance(String name) { List pipelineAggregators = new ArrayList<>(); // TODO populate pipelineAggregators Map metaData = new HashMap<>(); @@ -99,8 +103,7 @@ public abstract class InternalAggregationTestCase } /** Return an instance on an unmapped field. */ - protected final T createUnmappedInstance() { - String name = randomAsciiOfLength(5); + protected final T createUnmappedInstance(String name) { List pipelineAggregators = new ArrayList<>(); // TODO populate pipelineAggregators Map metaData = new HashMap<>(); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregationTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregationTestCase.java new file mode 100644 index 00000000000..6f8dac7eec4 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregationTestCase.java @@ -0,0 +1,85 @@ +/* + * 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; + +import org.elasticsearch.search.DocValueFormat; +import org.elasticsearch.search.aggregations.InternalAggregation; +import org.elasticsearch.search.aggregations.InternalAggregationTestCase; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.metrics.max.InternalMax; +import org.elasticsearch.search.aggregations.metrics.min.InternalMin; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; + +public abstract class InternalSingleBucketAggregationTestCase + extends InternalAggregationTestCase { + private final boolean hasInternalMax = randomBoolean(); + private final boolean hasInternalMin = randomBoolean(); + + protected abstract T createTestInstance(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData); + protected abstract void extraAssertReduced(T reduced, List inputs); + + @Override + protected final T createTestInstance(String name, List pipelineAggregators, Map metaData) { + List internal = new ArrayList<>(); + if (hasInternalMax) { + internal.add(new InternalMax("max", randomDouble(), + randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(), + emptyMap())); + } + if (hasInternalMin) { + internal.add(new InternalMin("min", randomDouble(), + randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), emptyList(), + emptyMap())); + } + // we shouldn't use the full long range here since we sum doc count on reduce, and don't want to overflow the long range there + long docCount = between(0, Integer.MAX_VALUE); + return createTestInstance(name, docCount, new InternalAggregations(internal), pipelineAggregators, metaData); + } + + @Override + protected final void assertReduced(T reduced, List inputs) { + assertEquals(inputs.stream().mapToLong(InternalSingleBucketAggregation::getDocCount).sum(), reduced.getDocCount()); + if (hasInternalMax) { + double expected = inputs.stream().mapToDouble(i -> { + InternalMax max = i.getAggregations().get("max"); + return max.getValue(); + }).max().getAsDouble(); + InternalMax reducedMax = reduced.getAggregations().get("max"); + assertEquals(expected, reducedMax.getValue(), 0); + } + if (hasInternalMin) { + double expected = inputs.stream().mapToDouble(i -> { + InternalMin min = i.getAggregations().get("min"); + return min.getValue(); + }).min().getAsDouble(); + InternalMin reducedMin = reduced.getAggregations().get("min"); + assertEquals(expected, reducedMin.getValue(), 0); + } + extraAssertReduced(reduced, inputs); + } +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/children/InternalChildrenTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/children/InternalChildrenTests.java index 3b54bf3f359..b248d5ed981 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/children/InternalChildrenTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/children/InternalChildrenTests.java @@ -20,42 +20,23 @@ package org.elasticsearch.search.aggregations.bucket.children; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.search.DocValueFormat; -import org.elasticsearch.search.aggregations.InternalAggregation; -import org.elasticsearch.search.aggregations.InternalAggregationTestCase; import org.elasticsearch.search.aggregations.InternalAggregations; -import org.elasticsearch.search.aggregations.metrics.max.InternalMax; +import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; -import java.util.ArrayList; import java.util.List; import java.util.Map; -public class InternalChildrenTests extends InternalAggregationTestCase { - +public class InternalChildrenTests extends InternalSingleBucketAggregationTestCase { @Override - protected InternalChildren createTestInstance(String name, List pipelineAggregators, - Map metaData) { - // we shouldn't use the full long range here since we sum doc count on reduce, and don't want to overflow the long range there - long docCount = randomIntBetween(0, Integer.MAX_VALUE); - int numAggregations = randomIntBetween(0, 20); - List aggs = new ArrayList<>(numAggregations); - for (int i = 0; i < numAggregations; i++) { - aggs.add(new InternalMax(randomAsciiOfLength(5), randomDouble(), - randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), pipelineAggregators, - metaData)); - } - // don't randomize the name parameter, since InternalSingleBucketAggregation#doReduce asserts its the same for all reduced aggs - return new InternalChildren("childAgg", docCount, new InternalAggregations(aggs), pipelineAggregators, metaData); + protected InternalChildren createTestInstance(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { + return new InternalChildren(name, docCount, aggregations, pipelineAggregators, metaData); } @Override - protected void assertReduced(InternalChildren reduced, List inputs) { - long expectedDocCount = 0; - for (Children input : inputs) { - expectedDocCount += input.getDocCount(); - } - assertEquals(expectedDocCount, reduced.getDocCount()); + protected void extraAssertReduced(InternalChildren reduced, List inputs) { + // Nothing extra to assert } @Override diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilterTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilterTests.java new file mode 100644 index 00000000000..3e74b9c2187 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/InternalFilterTests.java @@ -0,0 +1,46 @@ +/* + * 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.filter; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.List; +import java.util.Map; + +public class InternalFilterTests extends InternalSingleBucketAggregationTestCase { + @Override + protected InternalFilter createTestInstance(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { + return new InternalFilter(name, docCount, aggregations, pipelineAggregators, metaData); + } + + @Override + protected void extraAssertReduced(InternalFilter reduced, List inputs) { + // Nothing extra to assert + } + + @Override + protected Reader instanceReader() { + return InternalFilter::new; + } +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/global/InternalGlogbalTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/global/InternalGlogbalTests.java new file mode 100644 index 00000000000..0b55123a1ef --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/global/InternalGlogbalTests.java @@ -0,0 +1,47 @@ +/* + * 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.global; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.List; +import java.util.Map; + +public class InternalGlogbalTests extends InternalSingleBucketAggregationTestCase { + @Override + protected InternalGlobal createTestInstance(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { + return new InternalGlobal(name, docCount, aggregations, pipelineAggregators, metaData); + } + + @Override + protected void extraAssertReduced(InternalGlobal reduced, List inputs) { + // Nothing extra to assert + } + + @Override + protected Reader instanceReader() { + return InternalGlobal::new; + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissingTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissingTests.java new file mode 100644 index 00000000000..f3e151721bf --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/missing/InternalMissingTests.java @@ -0,0 +1,47 @@ +/* + * 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.missing; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.List; +import java.util.Map; + +public class InternalMissingTests extends InternalSingleBucketAggregationTestCase { + @Override + protected InternalMissing createTestInstance(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { + return new InternalMissing(name, docCount, aggregations, pipelineAggregators, metaData); + } + + @Override + protected void extraAssertReduced(InternalMissing reduced, List inputs) { + // Nothing extra to assert + } + + @Override + protected Reader instanceReader() { + return InternalMissing::new; + } + +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/InternalNestedTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/InternalNestedTests.java new file mode 100644 index 00000000000..7b410723666 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/InternalNestedTests.java @@ -0,0 +1,46 @@ +/* + * 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.nested; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.List; +import java.util.Map; + +public class InternalNestedTests extends InternalSingleBucketAggregationTestCase { + @Override + protected InternalNested createTestInstance(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { + return new InternalNested(name, docCount, aggregations, pipelineAggregators, metaData); + } + + @Override + protected void extraAssertReduced(InternalNested reduced, List inputs) { + // Nothing extra to assert + } + + @Override + protected Reader instanceReader() { + return InternalNested::new; + } +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNestedTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNestedTests.java new file mode 100644 index 00000000000..f918024733e --- /dev/null +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/InternalReverseNestedTests.java @@ -0,0 +1,46 @@ +/* + * 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.nested; + +import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregationTestCase; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; + +import java.util.List; +import java.util.Map; + +public class InternalReverseNestedTests extends InternalSingleBucketAggregationTestCase { + @Override + protected InternalReverseNested createTestInstance(String name, long docCount, InternalAggregations aggregations, + List pipelineAggregators, Map metaData) { + return new InternalReverseNested(name, docCount, aggregations, pipelineAggregators, metaData); + } + + @Override + protected void extraAssertReduced(InternalReverseNested reduced, List inputs) { + // Nothing extra to assert + } + + @Override + protected Reader instanceReader() { + return InternalReverseNested::new; + } +}