From 887ed68cf22d0b2a01a911c309007a9008aafc6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 14 Jul 2017 19:23:35 +0200 Subject: [PATCH] Fixing compilation issues and tests after merging in master --- .../index/rankeval/RatedRequest.java | 23 ++++--------------- .../rankeval/TransportRankEvalAction.java | 7 ++---- .../DiscountedCumulativeGainTests.java | 6 ++--- .../index/rankeval/PrecisionTests.java | 8 +++---- .../index/rankeval/RankEvalSpecTests.java | 20 ++++++---------- .../index/rankeval/RatedDocumentTests.java | 2 +- .../index/rankeval/RatedRequestsTests.java | 4 ++-- .../index/rankeval/ReciprocalRankTests.java | 6 ++--- 8 files changed, 27 insertions(+), 49 deletions(-) diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java index f673991a490..6d8bfd6485e 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java @@ -22,14 +22,12 @@ package org.elasticsearch.index.rankeval; import org.elasticsearch.action.support.ToXContentToBytes; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; @@ -236,7 +234,7 @@ public class RatedRequest extends ToXContentToBytes implements Writeable { private static final ParseField FIELDS_FIELD = new ParseField("summary_fields"); private static final ParseField TEMPLATE_ID_FIELD = new ParseField("template_id"); - private static final ConstructingObjectParser PARSER = + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("requests", a -> new RatedRequest((String) a[0], (List) a[1], (SearchSourceBuilder) a[2], (Map) a[3], (String) a[4])); @@ -246,20 +244,9 @@ public class RatedRequest extends ToXContentToBytes implements Writeable { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> { return RatedDocument.fromXContent(p); }, RATINGS_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { - try { - return SearchSourceBuilder.fromXContent(c); - } catch (IOException ex) { - throw new ParsingException(p.getTokenLocation(), "error parsing request", ex); - } - }, REQUEST_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { - try { - return (Map) p.map(); - } catch (IOException ex) { - throw new ParsingException(p.getTokenLocation(), "error parsing ratings", ex); - } - }, PARAMS_FIELD); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> + SearchSourceBuilder.fromXContent(p), REQUEST_FIELD); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> (Map) p.map(), PARAMS_FIELD); PARSER.declareStringArray(RatedRequest::setSummaryFields, FIELDS_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), TEMPLATE_ID_FIELD); } @@ -275,7 +262,7 @@ public class RatedRequest extends ToXContentToBytes implements Writeable { * "ratings": [{ "1": 1 }, { "2": 0 }, { "3": 1 } ] } */ public static RatedRequest fromXContent(XContentParser parser) { - return PARSER.apply(parser, new QueryParseContext(parser)); + return PARSER.apply(parser, null); } @Override diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java index 2be3ff4dadc..3336faea7d0 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java @@ -32,7 +32,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.TemplateScript; @@ -111,10 +110,8 @@ public class TransportRankEvalAction String templateId = ratedRequest.getTemplateId(); TemplateScript.Factory templateScript = scriptsWithoutParams.get(templateId); String resolvedRequest = templateScript.newInstance(params).execute(); - try (XContentParser subParser = createParser(namedXContentRegistry, new BytesArray(resolvedRequest), - XContentType.JSON)) { - QueryParseContext parseContext = new QueryParseContext(subParser); - ratedSearchSource = SearchSourceBuilder.fromXContent(parseContext); + try (XContentParser subParser = createParser(namedXContentRegistry, new BytesArray(resolvedRequest), XContentType.JSON)) { + ratedSearchSource = SearchSourceBuilder.fromXContent(subParser); } catch (IOException e) { listener.onFailure(e); } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java index 42c5ff113ff..707e63b0af1 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java @@ -62,7 +62,7 @@ public class DiscountedCumulativeGainTests extends ESTestCase { rated.add(new RatedDocument("index", "type", Integer.toString(i), relevanceRatings[i])); hits[i] = new SearchHit(i, Integer.toString(i), new Text("type"), Collections.emptyMap()); - hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0)); + hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0, null)); } DiscountedCumulativeGain dcg = new DiscountedCumulativeGain(); assertEquals(13.84826362927298, dcg.evaluate("id", hits, rated).getQualityLevel(), 0.00001); @@ -114,7 +114,7 @@ public class DiscountedCumulativeGainTests extends ESTestCase { } hits[i] = new SearchHit(i, Integer.toString(i), new Text("type"), Collections.emptyMap()); - hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0)); + hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0, null)); } DiscountedCumulativeGain dcg = new DiscountedCumulativeGain(); EvalQueryQuality result = dcg.evaluate("id", hits, rated); @@ -171,7 +171,7 @@ public class DiscountedCumulativeGainTests extends ESTestCase { for (int i = 0; i < 4; i++) { hits[i] = new SearchHit(i, Integer.toString(i), new Text("type"), Collections.emptyMap()); - hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0)); + hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0, null)); } DiscountedCumulativeGain dcg = new DiscountedCumulativeGain(); EvalQueryQuality result = dcg.evaluate("id", hits, ratedDocs); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionTests.java index be6a9b89c39..91868b32082 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionTests.java @@ -126,7 +126,7 @@ public class PrecisionTests extends ESTestCase { // add an unlabeled search hit SearchHit[] searchHits = Arrays.copyOf(toSearchHits(rated, "test", "testtype"), 3); searchHits[2] = new SearchHit(2, "2", new Text("testtype"), Collections.emptyMap()); - searchHits[2].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0)); + searchHits[2].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0, null)); EvalQueryQuality evaluated = (new Precision()).evaluate("id", searchHits, rated); assertEquals((double) 2 / 3, evaluated.getQualityLevel(), 0.00001); @@ -148,7 +148,7 @@ public class PrecisionTests extends ESTestCase { SearchHit[] hits = new SearchHit[5]; for (int i = 0; i < 5; i++) { hits[i] = new SearchHit(i, i + "", new Text("type"), Collections.emptyMap()); - hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0)); + hits[i].shard(new SearchShardTarget("testnode", new Index("index", "uuid"), 0, null)); } EvalQueryQuality evaluated = (new Precision()).evaluate("id", hits, Collections.emptyList()); @@ -227,7 +227,7 @@ public class PrecisionTests extends ESTestCase { RankEvalTestHelper.copy(testItem, Precision::new)); } - private Precision mutateTestItem(Precision original) { + private static Precision mutateTestItem(Precision original) { boolean ignoreUnlabeled = original.getIgnoreUnlabeled(); int relevantThreshold = original.getRelevantRatingThreshold(); Precision precision = new Precision(); @@ -246,7 +246,7 @@ public class PrecisionTests extends ESTestCase { SearchHit[] hits = new SearchHit[rated.size()]; for (int i = 0; i < rated.size(); i++) { hits[i] = new SearchHit(i, i + "", new Text(type), Collections.emptyMap()); - hits[i].shard(new SearchShardTarget("testnode", new Index(index, "uuid"), 0)); + hits[i].shard(new SearchShardTarget("testnode", new Index(index, "uuid"), 0, null)); } return hits; } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java index f8a879a74fb..75e72be2e12 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java @@ -68,24 +68,18 @@ public class RankEvalSpecTests extends ESTestCase { Collection templates = null; if (randomBoolean()) { - final Map params = randomBoolean() ? Collections.emptyMap() - : Collections.singletonMap("key", "value"); - ScriptType scriptType = randomFrom(ScriptType.values()); + final Map params = randomBoolean() ? Collections.emptyMap() : Collections.singletonMap("key", "value"); String script; - if (scriptType == ScriptType.INLINE) { - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - builder.startObject(); - builder.field("field", randomAlphaOfLengthBetween(1, 5)); - builder.endObject(); - script = builder.string(); - } - } else { - script = randomAlphaOfLengthBetween(1, 5); + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + builder.startObject(); + builder.field("field", randomAlphaOfLengthBetween(1, 5)); + builder.endObject(); + script = builder.string(); } templates = new HashSet<>(); templates.add(new ScriptWithId("templateId", - new Script(scriptType, Script.DEFAULT_TEMPLATE_LANG, script, params))); + new Script(ScriptType.INLINE, Script.DEFAULT_TEMPLATE_LANG, script, params))); Map templateParams = new HashMap<>(); templateParams.put("key", "value"); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java index 3afbcef2a02..9e0d16620d0 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java @@ -66,7 +66,7 @@ public class RatedDocumentTests extends ESTestCase { RankEvalTestHelper.copy(testItem, RatedDocument::new)); } - public void testInvalidParsing() throws IOException { + public void testInvalidParsing() { expectThrows(IllegalArgumentException.class, () -> new RatedDocument(null, "abc", "abc", 10)); expectThrows(IllegalArgumentException.class, () -> new RatedDocument("", "abc", "abc", 10)); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java index 5d5ae92d4be..7467763508d 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java @@ -86,7 +86,7 @@ public class RatedRequestsTests extends ESTestCase { SearchSourceBuilder testRequest = null; if (randomBoolean() || forceRequest) { testRequest = new SearchSourceBuilder(); - testRequest.size(randomInt()); + testRequest.size(randomIntBetween(0, Integer.MAX_VALUE)); testRequest.query(new MatchAllQueryBuilder()); } else { int randomSize = randomIntBetween(1, 10); @@ -198,7 +198,7 @@ public class RatedRequestsTests extends ESTestCase { new NamedWriteableRegistry(namedWriteables))); } - private RatedRequest mutateTestItem(RatedRequest original) { + private static RatedRequest mutateTestItem(RatedRequest original) { String id = original.getId(); SearchSourceBuilder testRequest = original.getTestRequest(); List ratedDocs = original.getRatedDocs(); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ReciprocalRankTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ReciprocalRankTests.java index 60eb6325a9a..4d0f3347acd 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ReciprocalRankTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ReciprocalRankTests.java @@ -156,12 +156,12 @@ public class ReciprocalRankTests extends ESTestCase { SearchHit[] hits = new SearchHit[to + 1 - from]; for (int i = from; i <= to; i++) { hits[i] = new SearchHit(i, i + "", new Text(type), Collections.emptyMap()); - hits[i].shard(new SearchShardTarget("testnode", new Index(index, "uuid"), 0)); + hits[i].shard(new SearchShardTarget("testnode", new Index(index, "uuid"), 0, null)); } return hits; } - private ReciprocalRank createTestItem() { + private static ReciprocalRank createTestItem() { ReciprocalRank testItem = new ReciprocalRank(); testItem.setRelevantRatingThreshhold(randomIntBetween(0, 20)); return testItem; @@ -182,7 +182,7 @@ public class ReciprocalRankTests extends ESTestCase { RankEvalTestHelper.copy(testItem, ReciprocalRank::new)); } - private ReciprocalRank mutateTestItem(ReciprocalRank testItem) { + private static ReciprocalRank mutateTestItem(ReciprocalRank testItem) { int relevantThreshold = testItem.getRelevantRatingThreshold(); ReciprocalRank rank = new ReciprocalRank(); rank.setRelevantRatingThreshhold(