From 6ba07dc78811818b32c12af35456ea16c3ea46f4 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Wed, 5 Jan 2022 16:46:27 +0100 Subject: Support an external list of terms in term list operators --- .../search/yql/ParameterListParserTestCase.java | 47 +++++++++++++ .../com/yahoo/search/yql/TermListTestCase.java | 78 ++++++++++++++++++++++ .../search/yql/YqlFieldAndSourceTestCase.java | 4 +- .../com/yahoo/search/yql/YqlParserTestCase.java | 1 + 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 container-search/src/test/java/com/yahoo/search/yql/ParameterListParserTestCase.java create mode 100644 container-search/src/test/java/com/yahoo/search/yql/TermListTestCase.java (limited to 'container-search/src/test/java/com') 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 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 55fb53b4460..ea807991bc3 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 @@ -1241,4 +1241,5 @@ public class YqlParserTestCase { actual.add(step.continuations().toString() + step.getOperation()); return actual.toString(); } + } -- cgit v1.2.3