From 5c9cc1d453297ad8e706f7800398e7610c70ef1f Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Wed, 24 Aug 2016 14:39:12 +0200 Subject: [PATCH] Add roundtripping to PrecisionAtN --- .../index/rankeval/PrecisionAtN.java | 21 ++++++++++++++++++- .../index/rankeval/PrecisionAtNTests.java | 10 +++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtN.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtN.java index c9ad3deb759..145270c15b1 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtN.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtN.java @@ -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 { @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); + } } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtNTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtNTests.java index d668b21630e..32b5c1cabc3 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtNTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtNTests.java @@ -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 { public void testPrecisionAtFiveCalculation() throws IOException, InterruptedException, ExecutionException { List 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); + } }