diff options
author | gjoranv <gjoranv@gmail.com> | 2022-11-02 15:36:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-02 15:36:32 +0100 |
commit | a84a2ae66cf8e4b4fed832985131dd6b8388e284 (patch) | |
tree | e7441a097ece39ad45de317c1f866405f4ff326d /client | |
parent | dd7ffce385159327aa84bfa02add535b0e4ba258 (diff) | |
parent | cecee424eb283929296a0933351d5488a4528ef1 (diff) |
Merge pull request #24709 from vespa-engine/bratseth/query-builder
Bratseth/query builder
Diffstat (limited to 'client')
7 files changed, 85 insertions, 142 deletions
diff --git a/client/src/main/java/ai/vespa/client/dsl/EndQuery.java b/client/src/main/java/ai/vespa/client/dsl/EndQuery.java index 61ffe84e5a9..d1d4b6eb883 100644 --- a/client/src/main/java/ai/vespa/client/dsl/EndQuery.java +++ b/client/src/main/java/ai/vespa/client/dsl/EndQuery.java @@ -76,15 +76,21 @@ public class EndQuery { } /** - * Semicolon. - * turn a query into fixed query. + * Calls fix() * - * @return the fixed query + * @deprecated use {link #fix} */ - public FixedQuery semicolon() { + @Deprecated // TODO: Remove on Vespa 9 + public FixedQuery semicolon() { return fix(); } + + /** Returns a fixed query containing this. */ + public FixedQuery fix() { return new FixedQuery(this); } + /** Calls fix().build() */ + public String build() { return fix().build(); } + /** * Group. * https://docs.vespa.ai/en/reference/query-language-reference.html#grouping @@ -163,7 +169,7 @@ public class EndQuery { StringBuilder sb = new StringBuilder(); String orderStr = order.stream().map(array -> A.empty().equals(array[0]) ? Text.format("%s %s", array[1], array[2]) - : Text.format("[%s]%s %s", array[0], array[1], array[2])) + : Text.format("%s%s %s", array[0], array[1], array[2])) .collect(Collectors.joining(", ")); String others = map.entrySet().stream() diff --git a/client/src/main/java/ai/vespa/client/dsl/FixedQuery.java b/client/src/main/java/ai/vespa/client/dsl/FixedQuery.java index d957217a9c7..b8fc3094937 100644 --- a/client/src/main/java/ai/vespa/client/dsl/FixedQuery.java +++ b/client/src/main/java/ai/vespa/client/dsl/FixedQuery.java @@ -332,7 +332,6 @@ public class FixedQuery { return this; } - /** * build the query map from the query * @@ -342,7 +341,6 @@ public class FixedQuery { if (queryMap != null) { return queryMap; } - assignIndex(); StringBuilder sb = new StringBuilder(); sb.append("select ") @@ -355,7 +353,6 @@ public class FixedQuery { if (!"".equals(endQuery.toString())) { sb.append(' ').append(endQuery); } - sb.append(";"); queryMap = new LinkedHashMap<>(); // for the order queryMap.put("yql", sb.toString()); @@ -374,23 +371,6 @@ public class FixedQuery { .collect(Collectors.joining("&")); } - private void assignIndex() { - assignIndex(endQuery.queryChain.getQuery(), new AtomicInteger()); - } - - private void assignIndex(QueryChain q, AtomicInteger ai) { - q.setIndex(ai.incrementAndGet()); - if (q instanceof Query) { - assignIndex((Query) q, ai); - } - } - - private void assignIndex(Query q, AtomicInteger ai) { - q.queries.stream() - .filter(QueryChain::nonEmpty) - .forEach(qu -> assignIndex(qu, ai)); - } - private Map<String, String> getUserInputs() { return getUserInputs(endQuery.queryChain.getQuery()); } diff --git a/client/src/main/java/ai/vespa/client/dsl/Query.java b/client/src/main/java/ai/vespa/client/dsl/Query.java index bc5be2280c4..36718ced814 100644 --- a/client/src/main/java/ai/vespa/client/dsl/Query.java +++ b/client/src/main/java/ai/vespa/client/dsl/Query.java @@ -290,13 +290,27 @@ public class Query extends QueryChain { } /** - * Semicolon. - * turn a query into fixed query. + * Calls fix() * * @return the fixed query + * @deprecated use {@link #fix()}, {@link #end()} or {@link #build} instead */ - public FixedQuery semicolon() { - return new FixedQuery(new EndQuery(this)); + @Deprecated // TODO: Remove on Vespa 9 + public FixedQuery semicolon() { return fix(); } + + /** Returns this as an ended query. */ + public EndQuery end() { + return new EndQuery(this); + } + + /** Calls end().fix(). */ + public FixedQuery fix() { + return end().fix(); + } + + /** Calls fix().build(). */ + public String build() { + return fix().build(); } @Override diff --git a/client/src/main/java/ai/vespa/client/dsl/QueryChain.java b/client/src/main/java/ai/vespa/client/dsl/QueryChain.java index 31b5220e871..58b1563a222 100644 --- a/client/src/main/java/ai/vespa/client/dsl/QueryChain.java +++ b/client/src/main/java/ai/vespa/client/dsl/QueryChain.java @@ -4,7 +4,6 @@ package ai.vespa.client.dsl; public abstract class QueryChain { String op; - int index; // for distinct each query chain Sources sources; Select select; Query query; @@ -18,10 +17,6 @@ public abstract class QueryChain { return op; } - void setIndex(int index) { - this.index = index; - } - Sources getSources() { return sources; } diff --git a/client/src/main/java/ai/vespa/client/dsl/Wand.java b/client/src/main/java/ai/vespa/client/dsl/Wand.java index 56bf3e3cf1d..df89d235139 100644 --- a/client/src/main/java/ai/vespa/client/dsl/Wand.java +++ b/client/src/main/java/ai/vespa/client/dsl/Wand.java @@ -66,7 +66,7 @@ public class Wand extends QueryChain { public String toString() { boolean hasAnnotation = A.hasAnnotation(annotation); String s = Text.format("wand(%s, %s)", fieldName, Q.toJson(value)); - return hasAnnotation ? Text.format("([%s]%s)", annotation, s) : s; + return hasAnnotation ? Text.format("(%s%s)", annotation, s) : s; } } diff --git a/client/src/main/java/ai/vespa/client/dsl/WeakAnd.java b/client/src/main/java/ai/vespa/client/dsl/WeakAnd.java index 45a616b5a8a..1e0ea8ad700 100644 --- a/client/src/main/java/ai/vespa/client/dsl/WeakAnd.java +++ b/client/src/main/java/ai/vespa/client/dsl/WeakAnd.java @@ -54,7 +54,7 @@ public class WeakAnd extends QueryChain { s = Text.format("weakAnd(%s)", value.queries.stream().map(Object::toString).collect(Collectors.joining(", "))); - return hasAnnotation ? Text.format("([%s]%s)", annotation, s) : s; + return hasAnnotation ? Text.format("(%s%s)", annotation, s) : s; } }
\ No newline at end of file diff --git a/client/src/test/java/ai/vespa/client/dsl/QTest.java b/client/src/test/java/ai/vespa/client/dsl/QTest.java index 114cbee0177..b3c26e3db6b 100644 --- a/client/src/test/java/ai/vespa/client/dsl/QTest.java +++ b/client/src/test/java/ai/vespa/client/dsl/QTest.java @@ -25,10 +25,9 @@ class QTest { String q = Q.select("f1", "f2") .from("sd1") .where("f1").contains("v1") - .semicolon() .build(); - assertEquals(q, "yql=select f1, f2 from sd1 where f1 contains \"v1\";"); + assertEquals(q, "yql=select f1, f2 from sd1 where f1 contains \"v1\""); } @Test @@ -36,10 +35,9 @@ class QTest { String q = Q.select("*") .from("sd1") .where("f1").contains("v1") - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 contains \"v1\";"); + assertEquals(q, "yql=select * from sd1 where f1 contains \"v1\""); } @Test @@ -47,10 +45,9 @@ class QTest { String q = Q.select("*") .from("sd1", "sd2") .where("f1").contains("v1") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources sd1, sd2 where f1 contains \"v1\";"); + assertEquals(q, "yql=select * from sources sd1, sd2 where f1 contains \"v1\""); } @Test @@ -66,11 +63,11 @@ class QTest { .timeout(3) .orderByDesc("f1") .orderByAsc("f2") - .semicolon() + .fix() .param("paramk1", "paramv1") .build(); - assertEquals(q, "yql=select * from sd1 where f1 contains \"v1\" and f2 contains \"v2\" or f3 contains \"v3\" and !(f4 contains \"v4\") order by f1 desc, f2 asc limit 2 offset 1 timeout 3;¶mk1=paramv1"); + assertEquals(q, "yql=select * from sd1 where f1 contains \"v1\" and f2 contains \"v2\" or f3 contains \"v3\" and !(f4 contains \"v4\") order by f1 desc, f2 asc limit 2 offset 1 timeout 3¶mk1=paramv1"); } @Test @@ -81,10 +78,9 @@ class QTest { .and("f2").matches("v2") .or("f3").matches("v3") .andnot("f4").matches("v4") - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 matches \"v1\" and f2 matches \"v2\" or f3 matches \"v3\" and !(f4 matches \"v4\");"); + assertEquals(q, "yql=select * from sd1 where f1 matches \"v1\" and f2 matches \"v2\" or f3 matches \"v3\" and !(f4 matches \"v4\")"); } @Test @@ -97,10 +93,9 @@ class QTest { .and("f4").gt(4) .and("f5").eq(5) .and("f6").inRange(6, 7) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 <= 1 and f2 < 2 and f3 >= 3 and f4 > 4 and f5 = 5 and range(f6, 6, 7);"); + assertEquals(q, "yql=select * from sd1 where f1 <= 1 and f2 < 2 and f3 >= 3 and f4 > 4 and f5 = 5 and range(f6, 6, 7)"); } @Test @@ -113,10 +108,9 @@ class QTest { .and("f4").gt(4L) .and("f5").eq(5L) .and("f6").inRange(6L, 7L) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 <= 1L and f2 < 2L and f3 >= 3L and f4 > 4L and f5 = 5L and range(f6, 6L, 7L);"); + assertEquals(q, "yql=select * from sd1 where f1 <= 1L and f2 < 2L and f3 >= 3L and f4 > 4L and f5 = 5L and range(f6, 6L, 7L)"); } @Test @@ -129,10 +123,9 @@ class QTest { .and("f4").gt(4.4) .and("f5").eq(5.5) .and("f6").inRange(6.6, 7.7) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 <= 1.1 and f2 < 2.2 and f3 >= 3.3 and f4 > 4.4 and f5 = 5.5 and range(f6, 6.6, 7.7);"); + assertEquals(q, "yql=select * from sd1 where f1 <= 1.1 and f2 < 2.2 and f3 >= 3.3 and f4 > 4.4 and f5 = 5.5 and range(f6, 6.6, 7.7)"); } @Test @@ -145,10 +138,9 @@ class QTest { .and("f4").gt(4.4D) .and("f5").eq(5.5D) .and("f6").inRange(6.6D, 7.7D) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 <= 1.1 and f2 < 2.2 and f3 >= 3.3 and f4 > 4.4 and f5 = 5.5 and range(f6, 6.6, 7.7);"); + assertEquals(q, "yql=select * from sd1 where f1 <= 1.1 and f2 < 2.2 and f3 >= 3.3 and f4 > 4.4 and f5 = 5.5 and range(f6, 6.6, 7.7)"); } @Test @@ -158,10 +150,9 @@ class QTest { .where("f1").contains("1") .andnot(Q.p(Q.p("f2").contains("2").and("f3").contains("3")) .or(Q.p("f2").contains("4").andnot("f3").contains("5"))) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 contains \"1\" and !((f2 contains \"2\" and f3 contains \"3\") or (f2 contains \"4\" and !(f3 contains \"5\")));"); + assertEquals(q, "yql=select * from sd1 where f1 contains \"1\" and !((f2 contains \"2\" and f3 contains \"3\") or (f2 contains \"4\" and !(f3 contains \"5\")))"); } @Test @@ -170,9 +161,8 @@ class QTest { .from("sd1") .where(Q.ui("value1")) .and(Q.ui("index", "value2")) - .semicolon() .build(); - assertEquals("yql=select * from sd1 where userInput(\"value1\") and ({\"defaultIndex\":\"index\"}userInput(\"value2\"));", q); + assertEquals("yql=select * from sd1 where userInput(\"value1\") and ({\"defaultIndex\":\"index\"}userInput(\"value2\"))", q); } @Test @@ -181,8 +171,8 @@ class QTest { .from("site") .where(Q.rank(Q.p("docQ").nearestNeighbor("vectorQuery"), Q.ui("@query"))) - .semicolon().build(); - assertEquals("yql=select url from site where rank(nearestNeighbor(docQ, vectorQuery), userInput(@query));", q); + .build(); + assertEquals("yql=select url from site where rank(nearestNeighbor(docQ, vectorQuery), userInput(@query))", q); } @Test @@ -191,10 +181,9 @@ class QTest { .from("sd1") .where(Q.dotPdt("f1", stringIntMap("a", 1, "b", 2, "c", 3))) .and("f2").contains("1") - .semicolon() .build(); - assertEquals("yql=select * from sd1 where dotProduct(f1, {\"a\":1,\"b\":2,\"c\":3}) and f2 contains \"1\";", q); + assertEquals("yql=select * from sd1 where dotProduct(f1, {\"a\":1,\"b\":2,\"c\":3}) and f2 contains \"1\"", q); } @Test @@ -203,10 +192,9 @@ class QTest { .from("sd1") .where(Q.wtdSet("f1", stringIntMap("a", 1, "b", 2, "c", 3))) .and("f2").contains("1") - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where weightedSet(f1, {\"a\":1,\"b\":2,\"c\":3}) and f2 contains \"1\";"); + assertEquals(q, "yql=select * from sd1 where weightedSet(f1, {\"a\":1,\"b\":2,\"c\":3}) and f2 contains \"1\""); } @Test @@ -215,10 +203,9 @@ class QTest { .from("sd1") .where(Q.nonEmpty(Q.p("f1").contains("v1"))) .and("f2").contains("v2") - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where nonEmpty(f1 contains \"v1\") and f2 contains \"v2\";"); + assertEquals(q, "yql=select * from sd1 where nonEmpty(f1 contains \"v1\") and f2 contains \"v2\""); } @@ -232,10 +219,9 @@ class QTest { Q.wand("f3", Arrays.asList(Arrays.asList(1, 1), Arrays.asList(2, 2))) .annotate(A.a("scoreThreshold", 0.13)) ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where wand(f1, {\"a\":1,\"b\":2,\"c\":3}) and wand(f2, [[1,1],[2,2]]) and ([{\"scoreThreshold\":0.13}]wand(f3, [[1,1],[2,2]]));"); + assertEquals(q, "yql=select * from sd1 where wand(f1, {\"a\":1,\"b\":2,\"c\":3}) and wand(f2, [[1,1],[2,2]]) and ({\"scoreThreshold\":0.13}wand(f3, [[1,1],[2,2]]))"); } @Test @@ -246,10 +232,9 @@ class QTest { .and(Q.weakand(Q.p("f1").contains("v1").and("f2").contains("v2")) .annotate(A.a("scoreThreshold", 0.13)) ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where weakAnd(f1 contains \"v1\", f2 contains \"v2\") and ([{\"scoreThreshold\":0.13}]weakAnd(f1 contains \"v1\", f2 contains \"v2\"));"); + assertEquals(q, "yql=select * from sd1 where weakAnd(f1 contains \"v1\", f2 contains \"v2\") and ({\"scoreThreshold\":0.13}weakAnd(f1 contains \"v1\", f2 contains \"v2\"))"); } @Test @@ -257,10 +242,9 @@ class QTest { String q = Q.select("*") .from("sd1") .where("a").contains("b").and(Q.geoLocation("taiwan", 25.105497, 121.597366, "200km")) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where a contains \"b\" and geoLocation(taiwan, 25.105497, 121.597366, \"200km\");"); + assertEquals(q, "yql=select * from sd1 where a contains \"b\" and geoLocation(taiwan, 25.105497, 121.597366, \"200km\")"); } @Test @@ -271,9 +255,8 @@ class QTest { .and(Q.nearestNeighbor("vec1", "vec2") .annotate(A.a("targetHits", 10, "approximate", false)) ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where a contains \"b\" and ([{\"approximate\":false,\"targetHits\":10}]nearestNeighbor(vec1, vec2));"); + assertEquals(q, "yql=select * from sd1 where a contains \"b\" and ([{\"approximate\":false,\"targetHits\":10}]nearestNeighbor(vec1, vec2))"); } @Test @@ -282,7 +265,6 @@ class QTest { () -> Q.select("*") .from("sd1") .where("a").contains("b").and(Q.nearestNeighbor("vec1", "vec2")) - .semicolon() .build()); } @@ -295,10 +277,9 @@ class QTest { Q.p("f1").contains("v1") ) ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where rank(f1 contains \"v1\");"); + assertEquals(q, "yql=select * from sd1 where rank(f1 contains \"v1\")"); } @Test @@ -310,10 +291,9 @@ class QTest { Q.p("f2").contains("v2"), Q.p("f3").eq(3)) ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where rank(f1 contains \"v1\", f2 contains \"v2\", f3 = 3);"); + assertEquals(q, "yql=select * from sd1 where rank(f1 contains \"v1\", f2 contains \"v2\", f3 = 3)"); } @Test @@ -325,25 +305,22 @@ class QTest { Q.p("f1").contains("v1"), ranks) ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where rank(f1 contains \"v1\", f2 contains \"v2\", f3 = 3);"); + assertEquals(q, "yql=select * from sd1 where rank(f1 contains \"v1\", f2 contains \"v2\", f3 = 3)"); } @Test void stringfunction_annotations() { - { Annotation annotation = A.filter(); String expected = "[{\"filter\":true}]"; String q = Q.select("*") .from("sd1") .where("f1").contains(annotation, "v1") - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 contains (" + expected + "\"v1\");"); + assertEquals(q, "yql=select * from sd1 where f1 contains (" + expected + "\"v1\")"); } { Annotation annotation = A.defaultIndex("idx"); @@ -351,10 +328,9 @@ class QTest { String q = Q.select("*") .from("sd1") .where("f1").contains(annotation, "v1") - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 contains (" + expected + "\"v1\");"); + assertEquals(q, "yql=select * from sd1 where f1 contains (" + expected + "\"v1\")"); } { Annotation annotation = A.a(stringObjMap("a1", stringObjMap("k1", "v1", "k2", 2))); @@ -362,10 +338,9 @@ class QTest { String q = Q.select("*") .from("sd1") .where("f1").contains(annotation, "v1") - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where f1 contains (" + expected + "\"v1\");"); + assertEquals(q, "yql=select * from sd1 where f1 contains (" + expected + "\"v1\")"); } } @@ -375,10 +350,9 @@ class QTest { String q = Q.select("*") .from("sd1") .where("f1").contains("v1").annotate(A.a("ak1", "av1")) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where ([{\"ak1\":\"av1\"}](f1 contains \"v1\"));"); + assertEquals(q, "yql=select * from sd1 where ([{\"ak1\":\"av1\"}](f1 contains \"v1\"))"); } @Test @@ -386,10 +360,9 @@ class QTest { String q = Q.select("*") .from("sd1") .where(Q.p("f1").contains("v1").annotate(A.a("ak1", "av1")).and("f2").contains("v2")) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where ([{\"ak1\":\"av1\"}](f1 contains \"v1\" and f2 contains \"v2\"));"); + assertEquals(q, "yql=select * from sd1 where ([{\"ak1\":\"av1\"}](f1 contains \"v1\" and f2 contains \"v2\"))"); } @Test @@ -400,19 +373,17 @@ class QTest { Q.p("f1").contains("v1").annotate(A.a("ak1", "av1"))) .and("f2").contains("v2") ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sd1 where (([{\"ak1\":\"av1\"}](f1 contains \"v1\")) and f2 contains \"v2\");"); + assertEquals(q, "yql=select * from sd1 where (([{\"ak1\":\"av1\"}](f1 contains \"v1\")) and f2 contains \"v2\")"); } @Test void build_query_which_created_from_Q_b_without_select_and_sources() { String q = Q.p("f1").contains("v1") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains \"v1\";"); + assertEquals(q, "yql=select * from sources * where f1 contains \"v1\""); } @Test @@ -422,126 +393,107 @@ class QTest { .orderByAsc(A.a(stringObjMap("function", "uca", "locale", "en_US", "strength", "IDENTICAL")), "f3") .orderByDesc("f4") .orderByDesc(A.a(stringObjMap("function", "lowercase")), "f5") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains \"v1\" order by f2 asc, [{\"function\":\"uca\",\"locale\":\"en_US\",\"strength\":\"IDENTICAL\"}]f3 asc, f4 desc, [{\"function\":\"lowercase\"}]f5 desc;"); + assertEquals(q, "yql=select * from sources * where f1 contains \"v1\" order by f2 asc, {\"function\":\"uca\",\"locale\":\"en_US\",\"strength\":\"IDENTICAL\"}f3 asc, f4 desc, {\"function\":\"lowercase\"}f5 desc"); } @Test void contains_sameElement() { String q = Q.p("f1").containsSameElement(Q.p("stime").le(1).and("etime").gt(2)) - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains sameElement(stime <= 1, etime > 2);"); + assertEquals(q, "yql=select * from sources * where f1 contains sameElement(stime <= 1, etime > 2)"); } @Test void contains_phrase_near_onear_equiv() { { String q1 = Q.p("f1").containsPhrase("p1", "p2", "p3") - .semicolon() .build(); String q2 = Q.p("f1").containsPhrase(Arrays.asList("p1", "p2", "p3")) - .semicolon() .build(); - assertEquals(q1, "yql=select * from sources * where f1 contains phrase(\"p1\", \"p2\", \"p3\");"); - assertEquals(q2, "yql=select * from sources * where f1 contains phrase(\"p1\", \"p2\", \"p3\");"); + assertEquals(q1, "yql=select * from sources * where f1 contains phrase(\"p1\", \"p2\", \"p3\")"); + assertEquals(q2, "yql=select * from sources * where f1 contains phrase(\"p1\", \"p2\", \"p3\")"); } { String q1 = Q.p("f1").containsNear("p1", "p2", "p3") - .semicolon() .build(); String q2 = Q.p("f1").containsNear(Arrays.asList("p1", "p2", "p3")) - .semicolon() .build(); - assertEquals(q1, "yql=select * from sources * where f1 contains near(\"p1\", \"p2\", \"p3\");"); - assertEquals(q2, "yql=select * from sources * where f1 contains near(\"p1\", \"p2\", \"p3\");"); + assertEquals(q1, "yql=select * from sources * where f1 contains near(\"p1\", \"p2\", \"p3\")"); + assertEquals(q2, "yql=select * from sources * where f1 contains near(\"p1\", \"p2\", \"p3\")"); } { String q1 = Q.p("f1").containsOnear("p1", "p2", "p3") - .semicolon() .build(); String q2 = Q.p("f1").containsOnear(Arrays.asList("p1", "p2", "p3")) - .semicolon() .build(); - assertEquals(q1, "yql=select * from sources * where f1 contains onear(\"p1\", \"p2\", \"p3\");"); - assertEquals(q2, "yql=select * from sources * where f1 contains onear(\"p1\", \"p2\", \"p3\");"); + assertEquals(q1, "yql=select * from sources * where f1 contains onear(\"p1\", \"p2\", \"p3\")"); + assertEquals(q2, "yql=select * from sources * where f1 contains onear(\"p1\", \"p2\", \"p3\")"); } { String q1 = Q.p("f1").containsEquiv("p1", "p2", "p3") - .semicolon() .build(); String q2 = Q.p("f1").containsEquiv(Arrays.asList("p1", "p2", "p3")) - .semicolon() .build(); - assertEquals(q1, "yql=select * from sources * where f1 contains equiv(\"p1\", \"p2\", \"p3\");"); - assertEquals(q2, "yql=select * from sources * where f1 contains equiv(\"p1\", \"p2\", \"p3\");"); + assertEquals(q1, "yql=select * from sources * where f1 contains equiv(\"p1\", \"p2\", \"p3\")"); + assertEquals(q2, "yql=select * from sources * where f1 contains equiv(\"p1\", \"p2\", \"p3\")"); } } @Test void contains_uri() { String q = Q.p("f1").containsUri("https://test.uri") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains uri(\"https://test.uri\");"); + assertEquals(q, "yql=select * from sources * where f1 contains uri(\"https://test.uri\")"); } @Test void contains_uri_with_annotation() { String q = Q.p("f1").containsUri(A.a("key", "value"), "https://test.uri") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains ([{\"key\":\"value\"}]uri(\"https://test.uri\"));"); + assertEquals(q, "yql=select * from sources * where f1 contains ([{\"key\":\"value\"}]uri(\"https://test.uri\"))"); } @Test void nearestNeighbor() { String q = Q.p("f1").nearestNeighbor("query_vector") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where nearestNeighbor(f1, query_vector);"); + assertEquals(q, "yql=select * from sources * where nearestNeighbor(f1, query_vector)"); } @Test void nearestNeighbor_with_annotation() { String q = Q.p("f1").nearestNeighbor(A.a("targetHits", 10), "query_vector") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where ([{\"targetHits\":10}]nearestNeighbor(f1, query_vector));"); + assertEquals(q, "yql=select * from sources * where ([{\"targetHits\":10}]nearestNeighbor(f1, query_vector))"); } @Test void use_contains_instead_of_contains_equiv_when_input_size_is_1() { String q = Q.p("f1").containsEquiv(Collections.singletonList("p1")) - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains \"p1\";"); + assertEquals(q, "yql=select * from sources * where f1 contains \"p1\""); } @Test void contains_phrase_near_onear_equiv_empty_list_should_throw_illegal_argument_exception() { assertThrows(IllegalArgumentException.class, () -> Q.p("f1").containsPhrase(Collections.emptyList()) - .semicolon() .build()); assertThrows(IllegalArgumentException.class, () -> Q.p("f1").containsNear(Collections.emptyList()) - .semicolon() .build()); assertThrows(IllegalArgumentException.class, () -> Q.p("f1").containsOnear(Collections.emptyList()) - .semicolon() .build()); assertThrows(IllegalArgumentException.class, () -> Q.p("f1").containsEquiv(Collections.emptyList()) - .semicolon() .build()); } @@ -550,17 +502,15 @@ class QTest { void contains_near_onear_with_annotation() { { String q = Q.p("f1").containsNear(A.a("distance", 5), "p1", "p2", "p3") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains ([{\"distance\":5}]near(\"p1\", \"p2\", \"p3\"));"); + assertEquals(q, "yql=select * from sources * where f1 contains ([{\"distance\":5}]near(\"p1\", \"p2\", \"p3\"))"); } { String q = Q.p("f1").containsOnear(A.a("distance", 5), "p1", "p2", "p3") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains ([{\"distance\":5}]onear(\"p1\", \"p2\", \"p3\"));"); + assertEquals(q, "yql=select * from sources * where f1 contains ([{\"distance\":5}]onear(\"p1\", \"p2\", \"p3\"))"); } } @@ -588,10 +538,9 @@ class QTest { )) )) ) - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains \"v1\" | all(group(a) max(5) each(output(count()) all(max(1) each(output(summary()))) all(group(b) each(output(count()) all(max(1) each(output(summary()))) all(group(c) each(output(count()) all(max(1) each(output(summary())))))))));"); + assertEquals(q, "yql=select * from sources * where f1 contains \"v1\" | all(group(a) max(5) each(output(count()) all(max(1) each(output(summary()))) all(group(b) each(output(count()) all(max(1) each(output(summary()))) all(group(c) each(output(count()) all(max(1) each(output(summary())))))))))"); } @Test @@ -608,10 +557,9 @@ class QTest { */ String q = Q.p("f1").contains("v1") .group("all(group(a) max(5) each(output(count()) all(max(1) each(output(summary()))) all(group(b) each(output(count()) all(max(1) each(output(summary()))) all(group(c) each(output(count()) all(max(1) each(output(summary())))))))))") - .semicolon() .build(); - assertEquals(q, "yql=select * from sources * where f1 contains \"v1\" | all(group(a) max(5) each(output(count()) all(max(1) each(output(summary()))) all(group(b) each(output(count()) all(max(1) each(output(summary()))) all(group(c) each(output(count()) all(max(1) each(output(summary())))))))));"); + assertEquals(q, "yql=select * from sources * where f1 contains \"v1\" | all(group(a) max(5) each(output(count()) all(max(1) each(output(summary()))) all(group(b) each(output(count()) all(max(1) each(output(summary()))) all(group(c) each(output(count()) all(max(1) each(output(summary())))))))))"); } @Test @@ -631,7 +579,7 @@ class QTest { .reduce(Query::and) .get(); - assertEquals(q.semicolon().build(), "yql=select * from sources * where a contains \"1\" and b contains \"2\" and c contains \"3\";"); + assertEquals(q.build(), "yql=select * from sources * where a contains \"1\" and b contains \"2\" and c contains \"3\""); } @Test @@ -641,14 +589,14 @@ class QTest { map.forEach((k, v) -> q.and(Q.p(k).contains(v))); - assertEquals(q.semicolon().build(), "yql=select * from sources * where a contains \"1\" and b contains \"2\" and c contains \"3\";"); + assertEquals(q.build(), "yql=select * from sources * where a contains \"1\" and b contains \"2\" and c contains \"3\""); } @Test void empty_queries_should_not_print_out() { - String q = Q.p(Q.p(Q.p().andnot(Q.p()).and(Q.p()))).and("a").contains("1").semicolon().build(); + String q = Q.p(Q.p(Q.p().andnot(Q.p()).and(Q.p()))).and("a").contains("1").build(); - assertEquals(q, "yql=select * from sources * where a contains \"1\";"); + assertEquals(q, "yql=select * from sources * where a contains \"1\""); } @Test |