Add roundtripping to PrecisionAtN

This commit is contained in:
Isabel Drost-Fromm 2016-08-24 14:39:12 +02:00
parent a2a92b9629
commit 5c9cc1d453
2 changed files with 28 additions and 3 deletions

View File

@ -33,6 +33,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.naming.directory.SearchResult;
@ -165,10 +166,28 @@ public class PrecisionAtN extends RankedListQualityMetric<PrecisionAtN> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
//builder.startObject(NAME); TODO Why does roundtripping fail with the name?
builder.startObject();
builder.field(SIZE_FIELD.getPreferredName(), this.n);
builder.endObject();
return builder;
}
@Override
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
PrecisionAtN other = (PrecisionAtN) obj;
return Objects.equals(n, other.n);
}
@Override
public final int hashCode() {
return Objects.hash(getClass(), n);
}
}

View File

@ -27,7 +27,6 @@ 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;
@ -38,7 +37,7 @@ import java.util.concurrent.ExecutionException;
import static java.util.Collections.emptyList;
public class PrecisionAtNTests extends ESTestCase {
public class PrecisionAtNTests extends XContentRoundtripTestCase<PrecisionAtN> {
public void testPrecisionAtFiveCalculation() throws IOException, InterruptedException, ExecutionException {
List<RatedDocument> rated = new ArrayList<>();
@ -111,4 +110,11 @@ public class PrecisionAtNTests extends ESTestCase {
partialResults.add(new EvalQueryQuality(0.6, emptyList()));
assertEquals(0.3, metric.combine(partialResults), Double.MIN_VALUE);
}
public void testXContentRoundtrip() throws IOException {
int position = randomIntBetween(0, 1000);
PrecisionAtN testItem = new PrecisionAtN(position);
roundtrip(testItem);
}
}