Skip to content

Commit fa10520

Browse files
author
Christoph Büscher
authored
[Test] Minor changes to rank_eval tests (#29577)
Removing an enum in favour of local constants to simplify tests and removing a few deprecated method calls and warnings.
1 parent e2d770d commit fa10520

File tree

7 files changed

+33
-52
lines changed

7 files changed

+33
-52
lines changed

modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private void assertParsedCorrect(String xContent, Integer expectedUnknownDocRati
253253

254254
public static DiscountedCumulativeGain createTestItem() {
255255
boolean normalize = randomBoolean();
256-
Integer unknownDocRating = new Integer(randomIntBetween(0, 1000));
256+
Integer unknownDocRating = Integer.valueOf(randomIntBetween(0, 1000));
257257

258258
return new DiscountedCumulativeGain(normalize, unknownDocRating, 10);
259259
}

modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646

4747
public class MeanReciprocalRankTests extends ESTestCase {
4848

49+
private static final int IRRELEVANT_RATING_0 = 0;
50+
private static final int RELEVANT_RATING_1 = 1;
51+
4952
public void testParseFromXContent() throws IOException {
5053
String xContent = "{ }";
5154
try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
@@ -84,9 +87,9 @@ public void testMaxAcceptableRank() {
8487
int relevantAt = randomIntBetween(0, searchHits);
8588
for (int i = 0; i <= searchHits; i++) {
8689
if (i == relevantAt) {
87-
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.RELEVANT.ordinal()));
90+
ratedDocs.add(new RatedDocument("test", Integer.toString(i), RELEVANT_RATING_1));
8891
} else {
89-
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.IRRELEVANT.ordinal()));
92+
ratedDocs.add(new RatedDocument("test", Integer.toString(i), IRRELEVANT_RATING_0));
9093
}
9194
}
9295

@@ -110,9 +113,9 @@ public void testEvaluationOneRelevantInResults() {
110113
int relevantAt = randomIntBetween(0, 9);
111114
for (int i = 0; i <= 20; i++) {
112115
if (i == relevantAt) {
113-
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.RELEVANT.ordinal()));
116+
ratedDocs.add(new RatedDocument("test", Integer.toString(i), RELEVANT_RATING_1));
114117
} else {
115-
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.IRRELEVANT.ordinal()));
118+
ratedDocs.add(new RatedDocument("test", Integer.toString(i), IRRELEVANT_RATING_0));
116119
}
117120
}
118121

modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@
4646

4747
public class PrecisionAtKTests extends ESTestCase {
4848

49+
private static final int IRRELEVANT_RATING_0 = 0;
50+
private static final int RELEVANT_RATING_1 = 1;
51+
4952
public void testPrecisionAtFiveCalculation() {
5053
List<RatedDocument> rated = new ArrayList<>();
51-
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
54+
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
5255
EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", toSearchHits(rated, "test"), rated);
5356
assertEquals(1, evaluated.getQualityLevel(), 0.00001);
5457
assertEquals(1, ((PrecisionAtK.Breakdown) evaluated.getMetricDetails()).getRelevantRetrieved());
@@ -57,11 +60,11 @@ public void testPrecisionAtFiveCalculation() {
5760

5861
public void testPrecisionAtFiveIgnoreOneResult() {
5962
List<RatedDocument> rated = new ArrayList<>();
60-
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
61-
rated.add(createRatedDoc("test", "1", TestRatingEnum.RELEVANT.ordinal()));
62-
rated.add(createRatedDoc("test", "2", TestRatingEnum.RELEVANT.ordinal()));
63-
rated.add(createRatedDoc("test", "3", TestRatingEnum.RELEVANT.ordinal()));
64-
rated.add(createRatedDoc("test", "4", TestRatingEnum.IRRELEVANT.ordinal()));
63+
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
64+
rated.add(createRatedDoc("test", "1", RELEVANT_RATING_1));
65+
rated.add(createRatedDoc("test", "2", RELEVANT_RATING_1));
66+
rated.add(createRatedDoc("test", "3", RELEVANT_RATING_1));
67+
rated.add(createRatedDoc("test", "4", IRRELEVANT_RATING_0));
6568
EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", toSearchHits(rated, "test"), rated);
6669
assertEquals((double) 4 / 5, evaluated.getQualityLevel(), 0.00001);
6770
assertEquals(4, ((PrecisionAtK.Breakdown) evaluated.getMetricDetails()).getRelevantRetrieved());
@@ -89,11 +92,11 @@ public void testPrecisionAtFiveRelevanceThreshold() {
8992

9093
public void testPrecisionAtFiveCorrectIndex() {
9194
List<RatedDocument> rated = new ArrayList<>();
92-
rated.add(createRatedDoc("test_other", "0", TestRatingEnum.RELEVANT.ordinal()));
93-
rated.add(createRatedDoc("test_other", "1", TestRatingEnum.RELEVANT.ordinal()));
94-
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
95-
rated.add(createRatedDoc("test", "1", TestRatingEnum.RELEVANT.ordinal()));
96-
rated.add(createRatedDoc("test", "2", TestRatingEnum.IRRELEVANT.ordinal()));
95+
rated.add(createRatedDoc("test_other", "0", RELEVANT_RATING_1));
96+
rated.add(createRatedDoc("test_other", "1", RELEVANT_RATING_1));
97+
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
98+
rated.add(createRatedDoc("test", "1", RELEVANT_RATING_1));
99+
rated.add(createRatedDoc("test", "2", IRRELEVANT_RATING_0));
97100
// the following search hits contain only the last three documents
98101
EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", toSearchHits(rated.subList(2, 5), "test"), rated);
99102
assertEquals((double) 2 / 3, evaluated.getQualityLevel(), 0.00001);
@@ -103,8 +106,8 @@ public void testPrecisionAtFiveCorrectIndex() {
103106

104107
public void testIgnoreUnlabeled() {
105108
List<RatedDocument> rated = new ArrayList<>();
106-
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
107-
rated.add(createRatedDoc("test", "1", TestRatingEnum.RELEVANT.ordinal()));
109+
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
110+
rated.add(createRatedDoc("test", "1", RELEVANT_RATING_1));
108111
// add an unlabeled search hit
109112
SearchHit[] searchHits = Arrays.copyOf(toSearchHits(rated, "test"), 3);
110113
searchHits[2] = new SearchHit(2, "2", new Text("testtype"), Collections.emptyMap());

modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalRequestIT.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
import static org.hamcrest.Matchers.instanceOf;
4444

4545
public class RankEvalRequestIT extends ESIntegTestCase {
46+
47+
private static final int RELEVANT_RATING_1 = 1;
48+
4649
@Override
4750
protected Collection<Class<? extends Plugin>> transportClientPlugins() {
4851
return Arrays.asList(RankEvalPlugin.class);
@@ -117,7 +120,7 @@ public void testPrecisionAtRequest() {
117120
if (id.equals("1") || id.equals("6")) {
118121
assertFalse(hit.getRating().isPresent());
119122
} else {
120-
assertEquals(TestRatingEnum.RELEVANT.ordinal(), hit.getRating().get().intValue());
123+
assertEquals(RELEVANT_RATING_1, hit.getRating().get().intValue());
121124
}
122125
}
123126
}
@@ -128,7 +131,7 @@ public void testPrecisionAtRequest() {
128131
for (RatedSearchHit hit : hitsAndRatings) {
129132
String id = hit.getSearchHit().getId();
130133
if (id.equals("1")) {
131-
assertEquals(TestRatingEnum.RELEVANT.ordinal(), hit.getRating().get().intValue());
134+
assertEquals(RELEVANT_RATING_1, hit.getRating().get().intValue());
132135
} else {
133136
assertFalse(hit.getRating().isPresent());
134137
}
@@ -259,7 +262,7 @@ public void testBadQuery() {
259262
public void testIndicesOptions() {
260263
SearchSourceBuilder amsterdamQuery = new SearchSourceBuilder().query(new MatchAllQueryBuilder());
261264
List<RatedDocument> relevantDocs = createRelevant("2", "3", "4", "5", "6");
262-
relevantDocs.add(new RatedDocument("test2", "7", TestRatingEnum.RELEVANT.ordinal()));
265+
relevantDocs.add(new RatedDocument("test2", "7", RELEVANT_RATING_1));
263266
List<RatedRequest> specifications = new ArrayList<>();
264267
specifications.add(new RatedRequest("amsterdam_query", relevantDocs, amsterdamQuery));
265268
RankEvalSpec task = new RankEvalSpec(specifications, new PrecisionAtK());
@@ -322,7 +325,7 @@ public void testIndicesOptions() {
322325
private static List<RatedDocument> createRelevant(String... docs) {
323326
List<RatedDocument> relevant = new ArrayList<>();
324327
for (String doc : docs) {
325-
relevant.add(new RatedDocument("test", doc, TestRatingEnum.RELEVANT.ordinal()));
328+
relevant.add(new RatedDocument("test", doc, RELEVANT_RATING_1));
326329
}
327330
return relevant;
328331
}

modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
5353
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
5454
import static org.hamcrest.Matchers.containsString;
55-
import static org.hamcrest.Matchers.startsWith;
5655

5756
public class RankEvalSpecTests extends ESTestCase {
5857

modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.index.rankeval;
2121

22-
import org.elasticsearch.ExceptionsHelper;
23-
import org.elasticsearch.common.ParsingException;
2422
import org.elasticsearch.common.bytes.BytesReference;
2523
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2624
import org.elasticsearch.common.settings.Settings;
@@ -54,7 +52,6 @@
5452
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
5553
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
5654
import static org.hamcrest.Matchers.containsString;
57-
import static org.hamcrest.Matchers.startsWith;
5855

5956
public class RatedRequestsTests extends ESTestCase {
6057

@@ -139,8 +136,8 @@ public void testXContentParsingIsNotLenient() throws IOException {
139136
Exception exception = expectThrows(Exception.class, () -> RatedRequest.fromXContent(parser));
140137
if (exception instanceof XContentParseException) {
141138
XContentParseException xcpe = (XContentParseException) exception;
142-
assertThat(ExceptionsHelper.detailedMessage(xcpe), containsString("unknown field"));
143-
assertThat(ExceptionsHelper.detailedMessage(xcpe), containsString("parser not found"));
139+
assertThat(xcpe.getCause().getMessage(), containsString("unknown field"));
140+
assertThat(xcpe.getCause().getMessage(), containsString("parser not found"));
144141
}
145142
if (exception instanceof XContentParseException) {
146143
assertThat(exception.getMessage(), containsString("[request] failed to parse field"));

modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/TestRatingEnum.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)