[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.
This commit is contained in:
Christoph Büscher 2018-04-19 13:50:18 +02:00 committed by GitHub
parent e2d770d9b9
commit fa1052017c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 52 deletions

View File

@ -253,7 +253,7 @@ public class DiscountedCumulativeGainTests extends ESTestCase {
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);
}

View File

@ -46,6 +46,9 @@ import static org.hamcrest.Matchers.startsWith;
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)) {
@ -84,9 +87,9 @@ public class MeanReciprocalRankTests extends ESTestCase {
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));
}
}
@ -110,9 +113,9 @@ public class MeanReciprocalRankTests extends ESTestCase {
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));
}
}

View File

@ -46,9 +46,12 @@ import static org.hamcrest.Matchers.startsWith;
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());
@ -57,11 +60,11 @@ public class PrecisionAtKTests extends ESTestCase {
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());
@ -89,11 +92,11 @@ public class PrecisionAtKTests extends ESTestCase {
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);
@ -103,8 +106,8 @@ public class PrecisionAtKTests extends ESTestCase {
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());

View File

@ -43,6 +43,9 @@ import static org.elasticsearch.index.rankeval.EvaluationMetric.filterUnknownDoc
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);
@ -117,7 +120,7 @@ public class RankEvalRequestIT extends ESIntegTestCase {
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());
}
}
}
@ -128,7 +131,7 @@ public class RankEvalRequestIT extends ESIntegTestCase {
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());
}
@ -259,7 +262,7 @@ public class RankEvalRequestIT extends ESIntegTestCase {
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());
@ -322,7 +325,7 @@ public class RankEvalRequestIT extends ESIntegTestCase {
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;
}

View File

@ -52,7 +52,6 @@ import java.util.function.Supplier;
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 {

View File

@ -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;
@ -54,7 +52,6 @@ import static java.util.stream.Collectors.toList;
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 {
@ -139,8 +136,8 @@ public class RatedRequestsTests extends ESTestCase {
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"));

View File

@ -1,24 +0,0 @@
/*
* 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.index.rankeval;
enum TestRatingEnum {
IRRELEVANT, RELEVANT;
}