aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/yql
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@oath.com>2022-01-07 22:45:30 +0100
committerGitHub <noreply@github.com>2022-01-07 22:45:30 +0100
commitfa8857b5f2e4c6c52620d7e82f3b6f635eee73fe (patch)
tree8e66c5d02dbc79c9ed1c5ed6f43e1514e93af5ad /container-search/src/test/java/com/yahoo/search/yql
parentc29684ae7128469f709fd3f3786d5eda8615fbf6 (diff)
parent75821899eca582886afe7a742876fb6aa58a05df (diff)
Merge pull request #20665 from vespa-engine/bratseth/termlist
Support an external list of terms in term list operators
Diffstat (limited to 'container-search/src/test/java/com/yahoo/search/yql')
-rw-r--r--container-search/src/test/java/com/yahoo/search/yql/ParameterListParserTestCase.java47
-rw-r--r--container-search/src/test/java/com/yahoo/search/yql/TermListTestCase.java78
-rw-r--r--container-search/src/test/java/com/yahoo/search/yql/YqlFieldAndSourceTestCase.java4
-rw-r--r--container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java1
4 files changed, 128 insertions, 2 deletions
diff --git a/container-search/src/test/java/com/yahoo/search/yql/ParameterListParserTestCase.java b/container-search/src/test/java/com/yahoo/search/yql/ParameterListParserTestCase.java
new file mode 100644
index 00000000000..44f784e96f3
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/yql/ParameterListParserTestCase.java
@@ -0,0 +1,47 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.yql;
+
+import com.yahoo.prelude.query.WeightedSetItem;
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author bratseth
+ */
+public class ParameterListParserTestCase {
+
+ @Test
+ public void testMapParsing() {
+ assertParsed("{}", Map.of());
+ assertParsed("{a:12}", Map.of("a", 12));
+ assertParsed("{'a':12}", Map.of("a", 12));
+ assertParsed("{\"a\":12}", Map.of("a", 12));
+ assertParsed("{a:12,b:13}", Map.of("a", 12, "b", 13));
+ assertParsed("{a:12, b:13}", Map.of("a", 12, "b", 13));
+ assertParsed(" { a:12, b:13} ", Map.of("a", 12, "b", 13));
+ assertParsed("{a:12, 'b':13} ", Map.of("a", 12, "b", 13));
+ assertParsed("{a:12,'b':13, \"c,}\": 14}", Map.of("a", 12, "b", 13, "c,}", 14));
+ }
+
+ @Test
+ public void testArrayParsing() {
+ assertParsed("[]", Map.of());
+ assertParsed("[[0,12]]", Map.of(0L, 12));
+ assertParsed("[[0,12],[1,13]]", Map.of(0L, 12, 1L, 13));
+ assertParsed("[[0,12], [1,13]]", Map.of(0L, 12, 1L, 13));
+ assertParsed(" [ [0,12], [ 1,13]] ", Map.of(0L, 12, 1L, 13));
+ }
+
+ private void assertParsed(String string, Map<?, Integer> expected) {
+ WeightedSetItem item = new WeightedSetItem("test");
+ ParameterListParser.addItemsFromString(string, item);
+ for (var entry : expected.entrySet()) {
+ assertEquals("Key '" + entry.getKey() + "'", entry.getValue(), item.getTokenWeight(entry.getKey()));
+ }
+ assertEquals("Token count is correct", expected.size(), item.getNumTokens());
+ }
+
+}
diff --git a/container-search/src/test/java/com/yahoo/search/yql/TermListTestCase.java b/container-search/src/test/java/com/yahoo/search/yql/TermListTestCase.java
new file mode 100644
index 00000000000..ac0d676caf7
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/yql/TermListTestCase.java
@@ -0,0 +1,78 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.yql;
+
+import com.yahoo.component.chain.Chain;
+import com.yahoo.search.Query;
+import com.yahoo.search.Result;
+import com.yahoo.search.searchchain.Execution;
+import org.apache.http.client.utils.URIBuilder;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Tests YQL expressions where a list of terms are supplied by indirection
+ *
+ * @author bratseth
+ */
+public class TermListTestCase {
+
+ @Test
+ public void testTermListInWeightedSet() {
+ URIBuilder builder = searchUri();
+ builder.setParameter("myTerms", "{'1':1, '2':1, '3':1}");
+ builder.setParameter("yql", "select * from sources * where weightedSet(user_id, @myTerms)");
+ Query query = searchAndAssertNoErrors(builder);
+ assertEquals("select * from sources * where weightedSet(user_id, {\"1\": 1, \"2\": 1, \"3\": 1});",
+ query.yqlRepresentation());
+ }
+
+ @Test
+ public void testTermListInWand() {
+ URIBuilder builder = searchUri();
+ builder.setParameter("myTerms", "{'1':1, '2':1, '3':1}");
+ builder.setParameter("yql", "select * from sources * where wand(user_id, @myTerms)");
+ Query query = searchAndAssertNoErrors(builder);
+ assertEquals("select * from sources * where wand(user_id, {\"1\": 1, \"2\": 1, \"3\": 1});",
+ query.yqlRepresentation());
+ }
+
+ @Test
+ public void testTermListInDotProduct() {
+ URIBuilder builder = searchUri();
+ builder.setParameter("myTerms", "{'1':1, '2':1, '3':1}");
+ builder.setParameter("yql", "select * from sources * where dotProduct(user_id, @myTerms)");
+ Query query = searchAndAssertNoErrors(builder);
+ assertEquals("select * from sources * where dotProduct(user_id, {\"1\": 1, \"2\": 1, \"3\": 1});",
+ query.yqlRepresentation());
+ }
+
+ private Query searchAndAssertNoErrors(URIBuilder builder) {
+ Query query = new Query(builder.toString());
+ var searchChain = new Chain<>(new MinimalQueryInserter());
+ var context = Execution.Context.createContextStub();
+ var execution = new Execution(searchChain, context);
+ Result r = execution.search(query);
+ var exception = exceptionOf(r);
+ assertNull(exception == null ? "No error":
+ exception.getMessage() + "\n" + Arrays.toString(exception.getStackTrace()),
+ r.hits().getError());
+ return query;
+ }
+
+ private Throwable exceptionOf(Result r) {
+ if (r.hits().getError() == null) return null;
+ if (r.hits().getError().getCause() == null) return null;
+ return r.hits().getError().getCause();
+ }
+
+ private URIBuilder searchUri() {
+ URIBuilder builder = new URIBuilder();
+ builder.setPath("search/");
+ return builder;
+ }
+
+}
diff --git a/container-search/src/test/java/com/yahoo/search/yql/YqlFieldAndSourceTestCase.java b/container-search/src/test/java/com/yahoo/search/yql/YqlFieldAndSourceTestCase.java
index 27959948536..5d3a95efc78 100644
--- a/container-search/src/test/java/com/yahoo/search/yql/YqlFieldAndSourceTestCase.java
+++ b/container-search/src/test/java/com/yahoo/search/yql/YqlFieldAndSourceTestCase.java
@@ -27,7 +27,7 @@ import com.yahoo.search.searchchain.testutil.DocumentSourceSearcher;
import static com.yahoo.search.searchchain.testutil.DocumentSourceSearcher.DEFAULT_SUMMARY_CLASS;;
/**
- * Test translation of fields and sources in YQL+ to the associated concepts in Vespa.
+ * Test translation of fields and sources in YQL to the associated concepts in Vespa.
*/
public class YqlFieldAndSourceTestCase {
@@ -40,7 +40,6 @@ public class YqlFieldAndSourceTestCase {
private Execution.Context context;
private Execution execution;
-
@Before
public void setUp() throws Exception {
Query query = new Query("?query=test");
@@ -137,6 +136,7 @@ public class YqlFieldAndSourceTestCase {
assertFalse(result.hits().get(0).isFilled(DEFAULT_SUMMARY_CLASS));
assertFalse(result.hits().get(0).isFilled(Execution.ATTRIBUTEPREFETCH));
}
+
@Test
public final void testTrivialCaseWithOnlyDiskfieldWrongClassRequested() {
final Query query = new Query("?query=test&presentation.summaryFields=" + FIELD1);
diff --git a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java
index 881101a7bda..636619bf1cc 100644
--- a/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java
+++ b/container-search/src/test/java/com/yahoo/search/yql/YqlParserTestCase.java
@@ -1253,4 +1253,5 @@ public class YqlParserTestCase {
actual.add(step.continuations().toString() + step.getOperation());
return actual.toString();
}
+
}