summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-02-28 14:31:22 +0100
committerJon Bratseth <bratseth@gmail.com>2022-02-28 14:31:22 +0100
commitefb422a36c76c77132a1cc7d534bdcc74644a018 (patch)
tree9ec0de7f327f7ccb2c0cbe7846c4eb165c741716
parent9775e3a373363ff4657c1361ebd1c3b6b5a20af6 (diff)
Restore
-rw-r--r--config-model/src/test/java/com/yahoo/searchdefinition/derived/GeminiTestCase.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/derived/GeminiTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/derived/GeminiTestCase.java
new file mode 100644
index 00000000000..07e6fbf7b1b
--- /dev/null
+++ b/config-model/src/test/java/com/yahoo/searchdefinition/derived/GeminiTestCase.java
@@ -0,0 +1,70 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.searchdefinition.derived;
+
+import com.yahoo.collections.Pair;
+import com.yahoo.searchdefinition.parser.ParseException;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author bratseth
+ */
+public class GeminiTestCase extends AbstractExportingTestCase {
+
+ @Test
+ public void testRanking2() throws IOException, ParseException {
+ DerivedConfiguration c = assertCorrectDeriving("gemini2");
+ RawRankProfile p = c.getRankProfileList().getRankProfile("test");
+ Map<String, String> ranking = removePartKeySuffixes(asMap(p.configProperties()));
+ assertEquals("attribute(right)", resolve(lookup("toplevel", ranking), ranking));
+ }
+
+ private Map<String, String> asMap(List<Pair<String, String>> properties) {
+ Map<String, String> map = new HashMap<>();
+ for (Pair<String, String> property : properties)
+ map.put(property.getFirst(), property.getSecond());
+ return map;
+ }
+
+ private Map<String, String> removePartKeySuffixes(Map<String, String> p) {
+ Map<String, String> pWithoutSuffixes = new HashMap<>();
+ for (Map.Entry<String, String> entry : p.entrySet())
+ pWithoutSuffixes.put(removePartSuffix(entry.getKey()), entry.getValue());
+ return pWithoutSuffixes;
+ }
+
+ private String removePartSuffix(String s) {
+ int partIndex = s.indexOf(".part");
+ if (partIndex <= 0) return s;
+ return s.substring(0, partIndex);
+ }
+
+ /**
+ * Recursively resolves references to other ranking expressions - rankingExpression(name) -
+ * and replaces the reference by the expression
+ */
+ private String resolve(String expression, Map<String, String> ranking) {
+ int referenceStartIndex;
+ while ((referenceStartIndex = expression.indexOf("rankingExpression(")) >= 0) {
+ int referenceEndIndex = expression.indexOf(")", referenceStartIndex);
+ expression = expression.substring(0, referenceStartIndex) +
+ resolve(lookup(expression.substring(referenceStartIndex + "rankingExpression(".length(), referenceEndIndex), ranking), ranking) +
+ expression.substring(referenceEndIndex + 1);
+ }
+ return expression;
+ }
+
+ private String lookup(String expressionName, Map<String, String> ranking) {
+ String value = ranking.get("rankingExpression(" + expressionName + ").rankingScript");
+ if (value == null) {
+ return expressionName;
+ }
+ return value;
+ }
+
+}