Skip to content

Commit

Permalink
[Test] Minor changes to rank_eval tests (#29577)
Browse files Browse the repository at this point in the history
Removing an enum in favour of local constants to simplify tests and
removing a few deprecated method calls and warnings.
  • Loading branch information
Christoph Büscher committed Apr 19, 2018
1 parent e2d770d commit fa10520
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private void assertParsedCorrect(String xContent, Integer expectedUnknownDocRati

public static DiscountedCumulativeGain createTestItem() {
boolean normalize = randomBoolean();
Integer unknownDocRating = new Integer(randomIntBetween(0, 1000));
Integer unknownDocRating = Integer.valueOf(randomIntBetween(0, 1000));

return new DiscountedCumulativeGain(normalize, unknownDocRating, 10);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

public class MeanReciprocalRankTests extends ESTestCase {

private static final int IRRELEVANT_RATING_0 = 0;
private static final int RELEVANT_RATING_1 = 1;

public void testParseFromXContent() throws IOException {
String xContent = "{ }";
try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
Expand Down Expand Up @@ -84,9 +87,9 @@ public void testMaxAcceptableRank() {
int relevantAt = randomIntBetween(0, searchHits);
for (int i = 0; i <= searchHits; i++) {
if (i == relevantAt) {
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.RELEVANT.ordinal()));
ratedDocs.add(new RatedDocument("test", Integer.toString(i), RELEVANT_RATING_1));
} else {
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.IRRELEVANT.ordinal()));
ratedDocs.add(new RatedDocument("test", Integer.toString(i), IRRELEVANT_RATING_0));
}
}

Expand All @@ -110,9 +113,9 @@ public void testEvaluationOneRelevantInResults() {
int relevantAt = randomIntBetween(0, 9);
for (int i = 0; i <= 20; i++) {
if (i == relevantAt) {
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.RELEVANT.ordinal()));
ratedDocs.add(new RatedDocument("test", Integer.toString(i), RELEVANT_RATING_1));
} else {
ratedDocs.add(new RatedDocument("test", Integer.toString(i), TestRatingEnum.IRRELEVANT.ordinal()));
ratedDocs.add(new RatedDocument("test", Integer.toString(i), IRRELEVANT_RATING_0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@

public class PrecisionAtKTests extends ESTestCase {

private static final int IRRELEVANT_RATING_0 = 0;
private static final int RELEVANT_RATING_1 = 1;

public void testPrecisionAtFiveCalculation() {
List<RatedDocument> rated = new ArrayList<>();
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", toSearchHits(rated, "test"), rated);
assertEquals(1, evaluated.getQualityLevel(), 0.00001);
assertEquals(1, ((PrecisionAtK.Breakdown) evaluated.getMetricDetails()).getRelevantRetrieved());
Expand All @@ -57,11 +60,11 @@ public void testPrecisionAtFiveCalculation() {

public void testPrecisionAtFiveIgnoreOneResult() {
List<RatedDocument> rated = new ArrayList<>();
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "1", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "2", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "3", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "4", TestRatingEnum.IRRELEVANT.ordinal()));
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "1", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "2", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "3", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "4", IRRELEVANT_RATING_0));
EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", toSearchHits(rated, "test"), rated);
assertEquals((double) 4 / 5, evaluated.getQualityLevel(), 0.00001);
assertEquals(4, ((PrecisionAtK.Breakdown) evaluated.getMetricDetails()).getRelevantRetrieved());
Expand Down Expand Up @@ -89,11 +92,11 @@ public void testPrecisionAtFiveRelevanceThreshold() {

public void testPrecisionAtFiveCorrectIndex() {
List<RatedDocument> rated = new ArrayList<>();
rated.add(createRatedDoc("test_other", "0", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test_other", "1", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "1", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "2", TestRatingEnum.IRRELEVANT.ordinal()));
rated.add(createRatedDoc("test_other", "0", RELEVANT_RATING_1));
rated.add(createRatedDoc("test_other", "1", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "1", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "2", IRRELEVANT_RATING_0));
// the following search hits contain only the last three documents
EvalQueryQuality evaluated = (new PrecisionAtK()).evaluate("id", toSearchHits(rated.subList(2, 5), "test"), rated);
assertEquals((double) 2 / 3, evaluated.getQualityLevel(), 0.00001);
Expand All @@ -103,8 +106,8 @@ public void testPrecisionAtFiveCorrectIndex() {

public void testIgnoreUnlabeled() {
List<RatedDocument> rated = new ArrayList<>();
rated.add(createRatedDoc("test", "0", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "1", TestRatingEnum.RELEVANT.ordinal()));
rated.add(createRatedDoc("test", "0", RELEVANT_RATING_1));
rated.add(createRatedDoc("test", "1", RELEVANT_RATING_1));
// add an unlabeled search hit
SearchHit[] searchHits = Arrays.copyOf(toSearchHits(rated, "test"), 3);
searchHits[2] = new SearchHit(2, "2", new Text("testtype"), Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import static org.hamcrest.Matchers.instanceOf;

public class RankEvalRequestIT extends ESIntegTestCase {

private static final int RELEVANT_RATING_1 = 1;

@Override
protected Collection<Class<? extends Plugin>> transportClientPlugins() {
return Arrays.asList(RankEvalPlugin.class);
Expand Down Expand Up @@ -117,7 +120,7 @@ public void testPrecisionAtRequest() {
if (id.equals("1") || id.equals("6")) {
assertFalse(hit.getRating().isPresent());
} else {
assertEquals(TestRatingEnum.RELEVANT.ordinal(), hit.getRating().get().intValue());
assertEquals(RELEVANT_RATING_1, hit.getRating().get().intValue());
}
}
}
Expand All @@ -128,7 +131,7 @@ public void testPrecisionAtRequest() {
for (RatedSearchHit hit : hitsAndRatings) {
String id = hit.getSearchHit().getId();
if (id.equals("1")) {
assertEquals(TestRatingEnum.RELEVANT.ordinal(), hit.getRating().get().intValue());
assertEquals(RELEVANT_RATING_1, hit.getRating().get().intValue());
} else {
assertFalse(hit.getRating().isPresent());
}
Expand Down Expand Up @@ -259,7 +262,7 @@ public void testBadQuery() {
public void testIndicesOptions() {
SearchSourceBuilder amsterdamQuery = new SearchSourceBuilder().query(new MatchAllQueryBuilder());
List<RatedDocument> relevantDocs = createRelevant("2", "3", "4", "5", "6");
relevantDocs.add(new RatedDocument("test2", "7", TestRatingEnum.RELEVANT.ordinal()));
relevantDocs.add(new RatedDocument("test2", "7", RELEVANT_RATING_1));
List<RatedRequest> specifications = new ArrayList<>();
specifications.add(new RatedRequest("amsterdam_query", relevantDocs, amsterdamQuery));
RankEvalSpec task = new RankEvalSpec(specifications, new PrecisionAtK());
Expand Down Expand Up @@ -322,7 +325,7 @@ public void testIndicesOptions() {
private static List<RatedDocument> createRelevant(String... docs) {
List<RatedDocument> relevant = new ArrayList<>();
for (String doc : docs) {
relevant.add(new RatedDocument("test", doc, TestRatingEnum.RELEVANT.ordinal()));
relevant.add(new RatedDocument("test", doc, RELEVANT_RATING_1));
}
return relevant;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;

public class RankEvalSpecTests extends ESTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package org.elasticsearch.index.rankeval;

import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -54,7 +52,6 @@
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;

public class RatedRequestsTests extends ESTestCase {

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

This file was deleted.

0 comments on commit fa10520

Please sign in to comment.