Add roundtripping to ReciprocalRank

This commit is contained in:
Isabel Drost-Fromm 2016-08-24 14:41:58 +02:00
parent 5c9cc1d453
commit cebb0ba0d8
3 changed files with 31 additions and 6 deletions

View File

@ -172,7 +172,7 @@ public class PrecisionAtN extends RankedListQualityMetric<PrecisionAtN> {
builder.endObject();
return builder;
}
@Override
public final boolean equals(Object obj) {
if (this == obj) {
@ -189,5 +189,4 @@ public class PrecisionAtN extends RankedListQualityMetric<PrecisionAtN> {
public final int hashCode() {
return Objects.hash(getClass(), n);
}
}

View File

@ -36,6 +36,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.naming.directory.SearchResult;
@ -159,10 +160,27 @@ public class ReciprocalRank extends RankedListQualityMetric<ReciprocalRank> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startObject(NAME);
//builder.startObject(NAME);
builder.field(MAX_RANK_FIELD.getPreferredName(), this.maxAcceptableRank);
builder.endObject();
//builder.endObject();
builder.endObject();
return builder;
}
@Override
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ReciprocalRank other = (ReciprocalRank) obj;
return Objects.equals(maxAcceptableRank, other.maxAcceptableRank);
}
@Override
public final int hashCode() {
return Objects.hash(getClass(), maxAcceptableRank);
}
}

View File

@ -24,8 +24,8 @@ import org.elasticsearch.index.Index;
import org.elasticsearch.index.rankeval.PrecisionAtN.Rating;
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.internal.InternalSearchHit;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -33,7 +33,7 @@ import java.util.Vector;
import static java.util.Collections.emptyList;
public class ReciprocalRankTests extends ESTestCase {
public class ReciprocalRankTests extends XContentRoundtripTestCase<ReciprocalRank> {
public void testMaxAcceptableRank() {
ReciprocalRank reciprocalRank = new ReciprocalRank();
@ -123,4 +123,12 @@ public class ReciprocalRankTests extends ESTestCase {
EvalQueryQuality evaluation = reciprocalRank.evaluate(hits, ratedDocs);
assertEquals(0.0, evaluation.getQualityLevel(), Double.MIN_VALUE);
}
public void testXContentRoundtrip() throws IOException {
int position = randomIntBetween(0, 1000);
ReciprocalRank testItem = new ReciprocalRank(position);
roundtrip(testItem);
}
}