From d5fc520741943c83cfac58b4f3289f65af233c6d Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Tue, 16 May 2017 14:54:42 +0200 Subject: [PATCH] Add parsing to Significant Terms aggregations (#24682) Related to #23331 --- .../significant/InternalSignificantTerms.java | 4 +- .../ParsedSignificantLongTerms.java | 87 +++++++++ .../ParsedSignificantStringTerms.java | 84 +++++++++ .../significant/ParsedSignificantTerms.java | 166 ++++++++++++++++++ .../aggregations/AggregationsTests.java | 4 + ...nternalMultiBucketAggregationTestCase.java | 29 +-- .../InternalSignificantTermsTestCase.java | 40 ++++- .../SignificantLongTermsTests.java | 21 ++- .../SignificantStringTermsTests.java | 18 +- .../test/InternalAggregationTestCase.java | 6 + 10 files changed, 432 insertions(+), 27 deletions(-) create mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantLongTerms.java create mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantStringTerms.java create mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantTerms.java diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/InternalSignificantTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/InternalSignificantTerms.java index acb655b19b4..ac60829c4ca 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/InternalSignificantTerms.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/InternalSignificantTerms.java @@ -44,8 +44,8 @@ import java.util.Objects; public abstract class InternalSignificantTerms, B extends InternalSignificantTerms.Bucket> extends InternalMultiBucketAggregation implements SignificantTerms, ToXContent { - private static final String SCORE = "score"; - private static final String BG_COUNT = "bg_count"; + public static final String SCORE = "score"; + public static final String BG_COUNT = "bg_count"; @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") public abstract static class Bucket> extends InternalMultiBucketAggregation.InternalBucket diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantLongTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantLongTerms.java new file mode 100644 index 00000000000..79e05f4be9a --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantLongTerms.java @@ -0,0 +1,87 @@ +/* + * 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.significant; + +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +public class ParsedSignificantLongTerms extends ParsedSignificantTerms { + + @Override + public String getType() { + return SignificantLongTerms.NAME; + } + + private static ObjectParser PARSER = + new ObjectParser<>(ParsedSignificantLongTerms.class.getSimpleName(), true, ParsedSignificantLongTerms::new); + static { + declareParsedSignificantTermsFields(PARSER, ParsedBucket::fromXContent); + } + + public static ParsedSignificantLongTerms fromXContent(XContentParser parser, String name) throws IOException { + ParsedSignificantLongTerms aggregation = PARSER.parse(parser, null); + aggregation.setName(name); + return aggregation; + } + + public static class ParsedBucket extends ParsedSignificantTerms.ParsedBucket { + + private Long key; + + @Override + public Object getKey() { + return key; + } + + @Override + public String getKeyAsString() { + String keyAsString = super.getKeyAsString(); + if (keyAsString != null) { + return keyAsString; + } + return Long.toString(key); + } + + public Number getKeyAsNumber() { + return key; + } + + @Override + public int compareTerm(SignificantTerms.Bucket other) { + return key.compareTo(((ParsedBucket) other).key); + } + + @Override + protected XContentBuilder keyToXContent(XContentBuilder builder) throws IOException { + builder.field(CommonFields.KEY.getPreferredName(), key); + if (super.getKeyAsString() != null) { + builder.field(CommonFields.KEY_AS_STRING.getPreferredName(), getKeyAsString()); + } + return builder; + } + + static ParsedBucket fromXContent(XContentParser parser) throws IOException { + return parseSignificantTermsBucketXContent(parser, new ParsedBucket(), (p, bucket) -> bucket.key = p.longValue()); + } + } +} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantStringTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantStringTerms.java new file mode 100644 index 00000000000..28d889b15c2 --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantStringTerms.java @@ -0,0 +1,84 @@ +/* + * 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.significant; + +import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +public class ParsedSignificantStringTerms extends ParsedSignificantTerms { + + @Override + public String getType() { + return SignificantStringTerms.NAME; + } + + private static ObjectParser PARSER = + new ObjectParser<>(ParsedSignificantStringTerms.class.getSimpleName(), true, ParsedSignificantStringTerms::new); + static { + declareParsedSignificantTermsFields(PARSER, ParsedBucket::fromXContent); + } + + public static ParsedSignificantStringTerms fromXContent(XContentParser parser, String name) throws IOException { + ParsedSignificantStringTerms aggregation = PARSER.parse(parser, null); + aggregation.setName(name); + return aggregation; + } + + public static class ParsedBucket extends ParsedSignificantTerms.ParsedBucket { + + private BytesRef key; + + @Override + public Object getKey() { + return getKeyAsString(); + } + + @Override + public String getKeyAsString() { + String keyAsString = super.getKeyAsString(); + if (keyAsString != null) { + return keyAsString; + } + return key.utf8ToString(); + } + + public Number getKeyAsNumber() { + return Double.parseDouble(key.utf8ToString()); + } + + @Override + public int compareTerm(SignificantTerms.Bucket other) { + return key.compareTo(((ParsedBucket) other).key); + } + + @Override + protected XContentBuilder keyToXContent(XContentBuilder builder) throws IOException { + return builder.field(CommonFields.KEY.getPreferredName(), getKey()); + } + + static ParsedBucket fromXContent(XContentParser parser) throws IOException { + return parseSignificantTermsBucketXContent(parser, new ParsedBucket(), (p, bucket) -> bucket.key = p.utf8BytesOrNull()); + } + } +} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantTerms.java new file mode 100644 index 00000000000..56be0aa6071 --- /dev/null +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/ParsedSignificantTerms.java @@ -0,0 +1,166 @@ +/* + * 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.significant; + +import org.elasticsearch.common.CheckedBiConsumer; +import org.elasticsearch.common.CheckedFunction; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.Aggregations; +import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public abstract class ParsedSignificantTerms extends ParsedMultiBucketAggregation + implements SignificantTerms { + + private Map bucketMap; + protected long subsetSize; + + protected long getSubsetSize() { + return subsetSize; + } + + @Override + public List getBuckets() { + return buckets; + } + + @Override + public SignificantTerms.Bucket getBucketByKey(String term) { + if (bucketMap == null) { + bucketMap = buckets.stream().collect(Collectors.toMap(SignificantTerms.Bucket::getKeyAsString, Function.identity())); + } + return bucketMap.get(term); + } + + @Override + public Iterator iterator() { + return buckets.stream().map(bucket -> (SignificantTerms.Bucket) bucket).collect(Collectors.toList()).iterator(); + } + + @Override + protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { + builder.field(CommonFields.DOC_COUNT.getPreferredName(), subsetSize); + builder.startArray(CommonFields.BUCKETS.getPreferredName()); + for (SignificantTerms.Bucket bucket : buckets) { + bucket.toXContent(builder, params); + } + builder.endArray(); + return builder; + } + + static void declareParsedSignificantTermsFields(final ObjectParser objectParser, + final CheckedFunction bucketParser) { + declareMultiBucketAggregationFields(objectParser, bucketParser::apply, bucketParser::apply); + objectParser.declareLong((parsedTerms, value) -> parsedTerms.subsetSize = value , CommonFields.DOC_COUNT); + } + + public abstract static class ParsedBucket extends ParsedMultiBucketAggregation.ParsedBucket implements SignificantTerms.Bucket { + + protected long subsetDf; + protected long supersetDf; + protected double score; + + @Override + public long getDocCount() { + return getSubsetDf(); + } + + @Override + public long getSubsetDf() { + return subsetDf; + } + + @Override + public long getSupersetDf() { + return supersetDf; + } + + @Override + public double getSignificanceScore() { + return score; + } + + @Override + public long getSupersetSize() { + throw new UnsupportedOperationException(); + } + + @Override + public long getSubsetSize() { + throw new UnsupportedOperationException(); + } + + @Override + public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + keyToXContent(builder); + builder.field(CommonFields.DOC_COUNT.getPreferredName(), getDocCount()); + builder.field(InternalSignificantTerms.SCORE, getSignificanceScore()); + builder.field(InternalSignificantTerms.BG_COUNT, getSupersetDf()); + getAggregations().toXContentInternal(builder, params); + builder.endObject(); + return builder; + } + + protected abstract XContentBuilder keyToXContent(XContentBuilder builder) throws IOException; + + static B parseSignificantTermsBucketXContent(final XContentParser parser, final B bucket, + final CheckedBiConsumer keyConsumer) throws IOException { + + final List aggregations = new ArrayList<>(); + XContentParser.Token token; + String currentFieldName = parser.currentName(); + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else if (token.isValue()) { + if (CommonFields.KEY_AS_STRING.getPreferredName().equals(currentFieldName)) { + bucket.setKeyAsString(parser.text()); + } else if (CommonFields.KEY.getPreferredName().equals(currentFieldName)) { + keyConsumer.accept(parser, bucket); + } else if (CommonFields.DOC_COUNT.getPreferredName().equals(currentFieldName)) { + long value = parser.longValue(); + bucket.subsetDf = value; + bucket.setDocCount(value); + } else if (InternalSignificantTerms.SCORE.equals(currentFieldName)) { + bucket.score = parser.longValue(); + } else if (InternalSignificantTerms.BG_COUNT.equals(currentFieldName)) { + bucket.supersetDf = parser.longValue(); + } + } else if (token == XContentParser.Token.START_OBJECT) { + aggregations.add(XContentParserUtils.parseTypedKeysObject(parser, Aggregation.TYPED_KEYS_DELIMITER, Aggregation.class)); + } + } + bucket.setAggregations(new Aggregations(aggregations)); + return bucket; + } + } +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/AggregationsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/AggregationsTests.java index fd4b5ae11c2..85deb604a66 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/AggregationsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/AggregationsTests.java @@ -40,6 +40,8 @@ import org.elasticsearch.search.aggregations.bucket.range.InternalRangeTests; import org.elasticsearch.search.aggregations.bucket.range.date.InternalDateRangeTests; import org.elasticsearch.search.aggregations.bucket.range.geodistance.InternalGeoDistanceTests; import org.elasticsearch.search.aggregations.bucket.sampler.InternalSamplerTests; +import org.elasticsearch.search.aggregations.bucket.significant.SignificantLongTermsTests; +import org.elasticsearch.search.aggregations.bucket.significant.SignificantStringTermsTests; import org.elasticsearch.search.aggregations.bucket.terms.DoubleTermsTests; import org.elasticsearch.search.aggregations.bucket.terms.LongTermsTests; import org.elasticsearch.search.aggregations.bucket.terms.StringTermsTests; @@ -126,6 +128,8 @@ public class AggregationsTests extends ESTestCase { aggsTests.add(new InternalGeoDistanceTests()); aggsTests.add(new InternalFiltersTests()); aggsTests.add(new InternalAdjacencyMatrixTests()); + aggsTests.add(new SignificantLongTermsTests()); + aggsTests.add(new SignificantStringTermsTests()); return Collections.unmodifiableList(aggsTests); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/InternalMultiBucketAggregationTestCase.java b/core/src/test/java/org/elasticsearch/search/aggregations/InternalMultiBucketAggregationTestCase.java index df977abbe81..38e8624eeb9 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/InternalMultiBucketAggregationTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/InternalMultiBucketAggregationTestCase.java @@ -67,28 +67,22 @@ public abstract class InternalMultiBucketAggregationTestCase parsedClass = implementationClass(); - assertTrue(parsedClass != null && parsedClass.isInstance(actual)); - - assertTrue(expected instanceof InternalAggregation && actual instanceof ParsedAggregation); - assertEquals(expected.getName(), actual.getName()); - assertEquals(expected.getMetaData(), actual.getMetaData()); - assertEquals(((InternalAggregation) expected).getType(), ((ParsedAggregation) actual).getType()); + assertMultiBucketsAggregation(expectedMultiBucketsAggregation, actualMultiBucketsAggregation, checkOrder); List expectedBuckets = expectedMultiBucketsAggregation.getBuckets(); List actualBuckets = actualMultiBucketsAggregation.getBuckets(); @@ -117,6 +111,17 @@ public abstract class InternalMultiBucketAggregationTestCase parsedClass = implementationClass(); + assertNotNull("Parsed aggregation class must not be null", parsedClass); + assertTrue(parsedClass.isInstance(actual)); + + assertTrue(expected instanceof InternalAggregation); + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getMetaData(), actual.getMetaData()); + assertEquals(((InternalAggregation) expected).getType(), ((ParsedAggregation) actual).getType()); + } + protected void assertBucket(MultiBucketsAggregation.Bucket expected, MultiBucketsAggregation.Bucket actual, boolean checkOrder) { assertTrue(expected instanceof InternalMultiBucketAggregation.InternalBucket); assertTrue(actual instanceof ParsedMultiBucketAggregation.ParsedBucket); @@ -136,13 +141,13 @@ public abstract class InternalMultiBucketAggregationTestCase> { +public abstract class InternalSignificantTermsTestCase extends InternalMultiBucketAggregationTestCase> { @Override protected InternalSignificantTerms createUnmappedInstance(String name, @@ -61,6 +62,41 @@ public abstract class InternalSignificantTermsTestCase extends InternalAggregati } } + @Override + protected void assertMultiBucketsAggregation(MultiBucketsAggregation expected, MultiBucketsAggregation actual, boolean checkOrder) { + super.assertMultiBucketsAggregation(expected, actual, checkOrder); + + assertTrue(expected instanceof InternalSignificantTerms); + assertTrue(actual instanceof ParsedSignificantTerms); + + InternalSignificantTerms expectedSigTerms = (InternalSignificantTerms) expected; + ParsedSignificantTerms actualSigTerms = (ParsedSignificantTerms) actual; + assertEquals(expectedSigTerms.getSubsetSize(), actualSigTerms.getSubsetSize()); + + for (SignificantTerms.Bucket bucket : (SignificantTerms) expected) { + String key = bucket.getKeyAsString(); + assertBucket(expectedSigTerms.getBucketByKey(key), actualSigTerms.getBucketByKey(key), checkOrder); + } + } + + @Override + protected void assertBucket(MultiBucketsAggregation.Bucket expected, MultiBucketsAggregation.Bucket actual, boolean checkOrder) { + super.assertBucket(expected, actual, checkOrder); + + assertTrue(expected instanceof InternalSignificantTerms.Bucket); + assertTrue(actual instanceof ParsedSignificantTerms.ParsedBucket); + + SignificantTerms.Bucket expectedSigTerm = (SignificantTerms.Bucket) expected; + SignificantTerms.Bucket actualSigTerm = (SignificantTerms.Bucket) actual; + + assertEquals(expectedSigTerm.getSignificanceScore(), actualSigTerm.getSignificanceScore(), 0.0); + assertEquals(expectedSigTerm.getSubsetDf(), actualSigTerm.getSubsetDf()); + assertEquals(expectedSigTerm.getSupersetDf(), actualSigTerm.getSupersetDf()); + + expectThrows(UnsupportedOperationException.class, actualSigTerm::getSubsetSize); + expectThrows(UnsupportedOperationException.class, actualSigTerm::getSupersetSize); + } + private static Map toCounts(Stream buckets, Function fn) { return buckets.collect(Collectors.toMap(SignificantTerms.Bucket::getKey, fn, Long::sum)); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTermsTests.java index 7e80cf61608..5b0e715c8a6 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTermsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantLongTermsTests.java @@ -21,6 +21,8 @@ package org.elasticsearch.search.aggregations.bucket.significant; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.search.DocValueFormat; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.GND; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.JLHScore; @@ -35,22 +37,24 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static org.elasticsearch.search.aggregations.InternalAggregations.EMPTY; - public class SignificantLongTermsTests extends InternalSignificantTermsTestCase { private SignificanceHeuristic significanceHeuristic; + private DocValueFormat format; + @Override @Before - public void setUpSignificanceHeuristic() { + public void setUp() throws Exception { + super.setUp(); significanceHeuristic = randomSignificanceHeuristic(); + format = randomNumericDocValueFormat(); } @Override protected InternalSignificantTerms createTestInstance(String name, List pipelineAggregators, - Map metaData) { - DocValueFormat format = DocValueFormat.RAW; + Map metaData, + InternalAggregations aggregations) { int requiredSize = randomIntBetween(1, 5); int shardSize = requiredSize + 2; final int numBuckets = randomInt(shardSize); @@ -70,7 +74,7 @@ public class SignificantLongTermsTests extends InternalSignificantTermsTestCase globalSubsetSize += subsetDf; globalSupersetSize += supersetSize; - buckets.add(new SignificantLongTerms.Bucket(subsetDf, subsetDf, supersetDf, supersetSize, term, EMPTY, format)); + buckets.add(new SignificantLongTerms.Bucket(subsetDf, subsetDf, supersetDf, supersetSize, term, aggregations, format)); } return new SignificantLongTerms(name, requiredSize, 1L, pipelineAggregators, metaData, format, globalSubsetSize, globalSupersetSize, significanceHeuristic, buckets); @@ -81,6 +85,11 @@ public class SignificantLongTermsTests extends InternalSignificantTermsTestCase return SignificantLongTerms::new; } + @Override + protected Class implementationClass() { + return ParsedSignificantLongTerms.class; + } + private static SignificanceHeuristic randomSignificanceHeuristic() { return randomFrom( new JLHScore(), diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantStringTermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantStringTermsTests.java index 82cd21cdf38..07fe4c1ae82 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantStringTermsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantStringTermsTests.java @@ -22,6 +22,8 @@ package org.elasticsearch.search.aggregations.bucket.significant; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.search.DocValueFormat; +import org.elasticsearch.search.aggregations.InternalAggregations; +import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.GND; import org.elasticsearch.search.aggregations.bucket.significant.heuristics.JLHScore; @@ -36,21 +38,22 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static org.elasticsearch.search.aggregations.InternalAggregations.EMPTY; - public class SignificantStringTermsTests extends InternalSignificantTermsTestCase { private SignificanceHeuristic significanceHeuristic; + @Override @Before - public void setUpSignificanceHeuristic() { + public void setUp() throws Exception { + super.setUp(); significanceHeuristic = randomSignificanceHeuristic(); } @Override protected InternalSignificantTerms createTestInstance(String name, List pipelineAggregators, - Map metaData) { + Map metaData, + InternalAggregations aggregations) { DocValueFormat format = DocValueFormat.RAW; int requiredSize = randomIntBetween(1, 5); int shardSize = requiredSize + 2; @@ -71,7 +74,7 @@ public class SignificantStringTermsTests extends InternalSignificantTermsTestCas globalSubsetSize += subsetDf; globalSupersetSize += supersetSize; - buckets.add(new SignificantStringTerms.Bucket(term, subsetDf, subsetDf, supersetDf, supersetSize, EMPTY, format)); + buckets.add(new SignificantStringTerms.Bucket(term, subsetDf, subsetDf, supersetDf, supersetSize, aggregations, format)); } return new SignificantStringTerms(name, requiredSize, 1L, pipelineAggregators, metaData, format, globalSubsetSize, globalSupersetSize, significanceHeuristic, buckets); @@ -82,6 +85,11 @@ public class SignificantStringTermsTests extends InternalSignificantTermsTestCas return SignificantStringTerms::new; } + @Override + protected Class implementationClass() { + return ParsedSignificantStringTerms.class; + } + private static SignificanceHeuristic randomSignificanceHeuristic() { return randomFrom( new JLHScore(), diff --git a/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java index 99b0046a887..c256275b994 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/InternalAggregationTestCase.java @@ -66,6 +66,10 @@ import org.elasticsearch.search.aggregations.bucket.range.geodistance.GeoDistanc import org.elasticsearch.search.aggregations.bucket.range.geodistance.ParsedGeoDistance; import org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler; import org.elasticsearch.search.aggregations.bucket.sampler.ParsedSampler; +import org.elasticsearch.search.aggregations.bucket.significant.ParsedSignificantLongTerms; +import org.elasticsearch.search.aggregations.bucket.significant.ParsedSignificantStringTerms; +import org.elasticsearch.search.aggregations.bucket.significant.SignificantLongTerms; +import org.elasticsearch.search.aggregations.bucket.significant.SignificantStringTerms; import org.elasticsearch.search.aggregations.bucket.terms.DoubleTerms; import org.elasticsearch.search.aggregations.bucket.terms.LongTerms; import org.elasticsearch.search.aggregations.bucket.terms.ParsedDoubleTerms; @@ -176,6 +180,8 @@ public abstract class InternalAggregationTestCase map.put(GeoDistanceAggregationBuilder.NAME, (p, c) -> ParsedGeoDistance.fromXContent(p, (String) c)); map.put(FiltersAggregationBuilder.NAME, (p, c) -> ParsedFilters.fromXContent(p, (String) c)); map.put(AdjacencyMatrixAggregationBuilder.NAME, (p, c) -> ParsedAdjacencyMatrix.fromXContent(p, (String) c)); + map.put(SignificantLongTerms.NAME, (p, c) -> ParsedSignificantLongTerms.fromXContent(p, (String) c)); + map.put(SignificantStringTerms.NAME, (p, c) -> ParsedSignificantStringTerms.fromXContent(p, (String) c)); namedXContents = map.entrySet().stream() .map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))