-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor significance heuristic tests for easier extensability (#75264)
The significant terms heuristic tests do not lend themselves well for new heuristics being added. This commit extracts common code and builds an abstract significant heuristic test class. This way new heuristics get the common suite of tests by extending a test class.
- Loading branch information
Showing
7 changed files
with
334 additions
and
186 deletions.
There are no files selected for viewing
281 changes: 104 additions & 177 deletions
281
...ket/terms/SignificanceHeuristicTests.java → ...s/AbstractSignificanceHeuristicTests.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...est/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/ChiSquareTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
public class ChiSquareTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new ChiSquare(randomBoolean(), randomBoolean()); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testBackgroundAssertions(new ChiSquare(true, true), new ChiSquare(true, false)); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
.../src/test/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/GNDTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class GNDTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new GND(randomBoolean()); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testBackgroundAssertions(new GND(true), new GND(false)); | ||
} | ||
|
||
/** | ||
* term is only in the subset, not at all in the other set but that is because the other set is empty. | ||
* this should actually not happen because only terms that are in the subset are considered now, | ||
* however, in this case the score should be 0 because a term that does not exist cannot be relevant... | ||
*/ | ||
public void testGNDCornerCases() { | ||
GND gnd = new GND(true); | ||
assertThat(gnd.getScore(0, randomIntBetween(1, 2), 0, randomIntBetween(2,3)), equalTo(0.0)); | ||
// the terms do not co-occur at all - should be 0 | ||
assertThat(gnd.getScore(0, randomIntBetween(1, 2), randomIntBetween(2, 3), randomIntBetween(5,6)), equalTo(0.0)); | ||
// comparison between two terms that do not exist - probably not relevant | ||
assertThat(gnd.getScore(0, 0, 0, randomIntBetween(1,2)), equalTo(0.0)); | ||
// terms co-occur perfectly - should be 1 | ||
assertThat(gnd.getScore(1, 1, 1, 1), equalTo(1.0)); | ||
gnd = new GND(false); | ||
assertThat(gnd.getScore(0, 0, 0, 0), equalTo(0.0)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...test/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/JLHScoreTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
public class JLHScoreTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new JLHScore(); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testAssertions(new JLHScore()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../org/elasticsearch/search/aggregations/bucket/terms/heuristic/MutualInformationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.hamcrest.Matchers.lessThan; | ||
import static org.hamcrest.Matchers.lessThanOrEqualTo; | ||
|
||
public class MutualInformationTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new MutualInformation(randomBoolean(), randomBoolean()); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testBackgroundAssertions(new MutualInformation(true, true), new MutualInformation(true, false)); | ||
} | ||
|
||
public void testScoreMutual() { | ||
SignificanceHeuristic heuristic = new MutualInformation(true, true); | ||
assertThat(heuristic.getScore(1, 1, 1, 3), greaterThan(0.0)); | ||
assertThat(heuristic.getScore(1, 1, 2, 3), lessThan(heuristic.getScore(1, 1, 1, 3))); | ||
assertThat(heuristic.getScore(2, 2, 2, 4), equalTo(1.0)); | ||
assertThat(heuristic.getScore(0, 2, 2, 4), equalTo(1.0)); | ||
assertThat(heuristic.getScore(2, 2, 4, 4), equalTo(0.0)); | ||
assertThat(heuristic.getScore(1, 2, 2, 4), equalTo(0.0)); | ||
assertThat(heuristic.getScore(3, 6, 9, 18), equalTo(0.0)); | ||
|
||
double score = 0.0; | ||
try { | ||
long a = randomLong(); | ||
long b = randomLong(); | ||
long c = randomLong(); | ||
long d = randomLong(); | ||
score = heuristic.getScore(a, b, c, d); | ||
} catch (IllegalArgumentException e) { | ||
} | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
heuristic = new MutualInformation(false, true); | ||
assertThat(heuristic.getScore(0, 1, 2, 3), equalTo(Double.NEGATIVE_INFINITY)); | ||
|
||
heuristic = new MutualInformation(true, false); | ||
score = heuristic.getScore(2, 3, 1, 4); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
score = heuristic.getScore(1, 4, 2, 3); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
score = heuristic.getScore(1, 3, 4, 4); | ||
assertThat(score, greaterThanOrEqualTo(0.0)); | ||
assertThat(score, lessThanOrEqualTo(1.0)); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...va/org/elasticsearch/search/aggregations/bucket/terms/heuristic/PercentageScoreTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket.terms.heuristic; | ||
|
||
import org.elasticsearch.search.aggregations.bucket.terms.AbstractSignificanceHeuristicTests; | ||
|
||
public class PercentageScoreTests extends AbstractSignificanceHeuristicTests { | ||
@Override | ||
protected SignificanceHeuristic getHeuristic() { | ||
return new PercentageScore(); | ||
} | ||
|
||
@Override | ||
protected boolean testZeroScore() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void testAssertions() { | ||
testAssertions(new PercentageScore()); | ||
} | ||
} |