|
@@ -19,8 +19,14 @@
|
|
|
|
|
|
package org.elasticsearch.search.aggregations.bucket.significant;
|
|
|
|
|
|
+import org.elasticsearch.search.aggregations.InternalAggregations;
|
|
|
import org.elasticsearch.search.aggregations.InternalMultiBucketAggregationTestCase;
|
|
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
|
|
|
+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;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.significant.heuristics.MutualInformation;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristic;
|
|
|
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
|
|
|
|
|
import java.util.Arrays;
|
|
@@ -33,6 +39,51 @@ import java.util.stream.Stream;
|
|
|
|
|
|
public abstract class InternalSignificantTermsTestCase extends InternalMultiBucketAggregationTestCase<InternalSignificantTerms<?, ?>> {
|
|
|
|
|
|
+ private SignificanceHeuristic significanceHeuristic;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setUp() throws Exception {
|
|
|
+ super.setUp();
|
|
|
+ significanceHeuristic = randomSignificanceHeuristic();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected final InternalSignificantTerms createTestInstance(String name,
|
|
|
+ List<PipelineAggregator> pipelineAggregators,
|
|
|
+ Map<String, Object> metaData,
|
|
|
+ InternalAggregations aggregations) {
|
|
|
+ final int requiredSize = randomIntBetween(1, 5);
|
|
|
+ final int numBuckets = randomInt(requiredSize + 2);
|
|
|
+
|
|
|
+ long subsetSize = 0;
|
|
|
+ long supersetSize = 0;
|
|
|
+
|
|
|
+ int[] subsetDfs = new int[numBuckets];
|
|
|
+ int[] supersetDfs = new int[numBuckets];
|
|
|
+
|
|
|
+ for (int i = 0; i < numBuckets; ++i) {
|
|
|
+ int subsetDf = randomIntBetween(1, 10);
|
|
|
+ subsetDfs[i] = subsetDf;
|
|
|
+
|
|
|
+ int supersetDf = randomIntBetween(subsetDf, 20);
|
|
|
+ supersetDfs[i] = supersetDf;
|
|
|
+
|
|
|
+ subsetSize += subsetDf;
|
|
|
+ supersetSize += supersetDf;
|
|
|
+ }
|
|
|
+ return createTestInstance(name, pipelineAggregators, metaData, aggregations, requiredSize, numBuckets, subsetSize, subsetDfs,
|
|
|
+ supersetSize, supersetDfs, significanceHeuristic);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected abstract InternalSignificantTerms createTestInstance(String name,
|
|
|
+ List<PipelineAggregator> pipelineAggregators,
|
|
|
+ Map<String, Object> metaData,
|
|
|
+ InternalAggregations aggregations,
|
|
|
+ int requiredSize, int numBuckets,
|
|
|
+ long subsetSize, int[] subsetDfs,
|
|
|
+ long supersetSize, int[] supersetDfs,
|
|
|
+ SignificanceHeuristic significanceHeuristic);
|
|
|
+
|
|
|
@Override
|
|
|
protected InternalSignificantTerms createUnmappedInstance(String name,
|
|
|
List<PipelineAggregator> pipelineAggregators,
|
|
@@ -72,6 +123,7 @@ public abstract class InternalSignificantTermsTestCase extends InternalMultiBuck
|
|
|
InternalSignificantTerms expectedSigTerms = (InternalSignificantTerms) expected;
|
|
|
ParsedSignificantTerms actualSigTerms = (ParsedSignificantTerms) actual;
|
|
|
assertEquals(expectedSigTerms.getSubsetSize(), actualSigTerms.getSubsetSize());
|
|
|
+ assertEquals(expectedSigTerms.getSupersetSize(), actualSigTerms.getSupersetSize());
|
|
|
|
|
|
for (SignificantTerms.Bucket bucket : (SignificantTerms) expected) {
|
|
|
String key = bucket.getKeyAsString();
|
|
@@ -91,14 +143,22 @@ public abstract class InternalSignificantTermsTestCase extends InternalMultiBuck
|
|
|
|
|
|
assertEquals(expectedSigTerm.getSignificanceScore(), actualSigTerm.getSignificanceScore(), 0.0);
|
|
|
assertEquals(expectedSigTerm.getSubsetDf(), actualSigTerm.getSubsetDf());
|
|
|
+ assertEquals(expectedSigTerm.getDocCount(), actualSigTerm.getSubsetDf());
|
|
|
assertEquals(expectedSigTerm.getSupersetDf(), actualSigTerm.getSupersetDf());
|
|
|
-
|
|
|
- expectThrows(UnsupportedOperationException.class, actualSigTerm::getSubsetSize);
|
|
|
- expectThrows(UnsupportedOperationException.class, actualSigTerm::getSupersetSize);
|
|
|
+ assertEquals(expectedSigTerm.getSubsetSize(), actualSigTerm.getSubsetSize());
|
|
|
+ assertEquals(expectedSigTerm.getSupersetSize(), actualSigTerm.getSupersetSize());
|
|
|
}
|
|
|
|
|
|
private static Map<Object, Long> toCounts(Stream<? extends SignificantTerms.Bucket> buckets,
|
|
|
Function<SignificantTerms.Bucket, Long> fn) {
|
|
|
return buckets.collect(Collectors.toMap(SignificantTerms.Bucket::getKey, fn, Long::sum));
|
|
|
}
|
|
|
+
|
|
|
+ private static SignificanceHeuristic randomSignificanceHeuristic() {
|
|
|
+ return randomFrom(
|
|
|
+ new JLHScore(),
|
|
|
+ new MutualInformation(randomBoolean(), randomBoolean()),
|
|
|
+ new GND(randomBoolean()),
|
|
|
+ new ChiSquare(randomBoolean(), randomBoolean()));
|
|
|
+ }
|
|
|
}
|