From 4f6b6585cbcd7340c1b89ed3512c35b97f33bd7a Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Fri, 28 Oct 2022 16:09:25 +0200 Subject: Improvements to iserInput handling - Handle both direct values and references in userInput - Don't convert direct values to references - Accept any QueryChain in rank --- .../main/java/ai/vespa/client/dsl/FixedQuery.java | 9 ++---- client/src/main/java/ai/vespa/client/dsl/Q.java | 2 +- client/src/main/java/ai/vespa/client/dsl/Rank.java | 4 +-- .../main/java/ai/vespa/client/dsl/UserInput.java | 33 ++++++++++------------ 4 files changed, 21 insertions(+), 27 deletions(-) (limited to 'client/src/main') 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 9976a405182..d957217a9c7 100644 --- a/client/src/main/java/ai/vespa/client/dsl/FixedQuery.java +++ b/client/src/main/java/ai/vespa/client/dsl/FixedQuery.java @@ -9,7 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; /** - * FixedQuery contains a 'Query' which is terminated by a 'semicolon' + * FixedQuery contains a 'Query'. * This object holds vespa or user defined parameters * https://docs.vespa.ai/en/reference/query-api-reference.html */ @@ -342,7 +342,6 @@ public class FixedQuery { if (queryMap != null) { return queryMap; } - assignIndex(); StringBuilder sb = new StringBuilder(); @@ -366,7 +365,7 @@ public class FixedQuery { } /** - * build the vespa query string join by '&' + * Builds the vespa query string joined by '&' * * @return the query string */ @@ -399,9 +398,7 @@ public class FixedQuery { private Map getUserInputs(Query q) { Map param = new HashMap<>(); q.queries.forEach(qu -> { - if (qu instanceof UserInput) { - param.putAll(((UserInput) qu).getParam()); - } else if (qu instanceof Query) { + if (qu instanceof Query) { param.putAll(getUserInputs((Query) qu)); } }); diff --git a/client/src/main/java/ai/vespa/client/dsl/Q.java b/client/src/main/java/ai/vespa/client/dsl/Q.java index 70e0e644c07..2bb998cd3e5 100644 --- a/client/src/main/java/ai/vespa/client/dsl/Q.java +++ b/client/src/main/java/ai/vespa/client/dsl/Q.java @@ -69,7 +69,7 @@ public final class Q { * @param ranks the ranks * @return the rank query */ - public static Rank rank(Query query, Query... ranks) { + public static Rank rank(Query query, QueryChain... ranks) { return new Rank(query, ranks); } diff --git a/client/src/main/java/ai/vespa/client/dsl/Rank.java b/client/src/main/java/ai/vespa/client/dsl/Rank.java index 86fe80b2909..aad70a29502 100644 --- a/client/src/main/java/ai/vespa/client/dsl/Rank.java +++ b/client/src/main/java/ai/vespa/client/dsl/Rank.java @@ -9,9 +9,9 @@ import java.util.stream.Stream; public class Rank extends QueryChain { - private final List queries = new ArrayList<>(); + private final List queries = new ArrayList<>(); - Rank(Query query, Query... ranks) { + Rank(Query query, QueryChain... ranks) { this.query = query; this.nonEmpty = query.nonEmpty(); queries.add(query); diff --git a/client/src/main/java/ai/vespa/client/dsl/UserInput.java b/client/src/main/java/ai/vespa/client/dsl/UserInput.java index f42b3d82b52..de048c57574 100644 --- a/client/src/main/java/ai/vespa/client/dsl/UserInput.java +++ b/client/src/main/java/ai/vespa/client/dsl/UserInput.java @@ -1,16 +1,14 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.client.dsl; -import java.util.Collections; -import java.util.Map; import java.util.UUID; public class UserInput extends QueryChain { private final Annotation annotation; // accept only defaultIndex annotation private final String value; + private final boolean valueIsReference; private final String indexField; - private String placeholder; // for generating unique param private boolean setDefaultIndex; UserInput(Sources sources, String value) { @@ -21,6 +19,7 @@ public class UserInput extends QueryChain { this.sources = sources; this.annotation = annotation; this.value = value; + this.valueIsReference = value.startsWith("@"); this.nonEmpty = true; if (annotation.contains("defaultIndex")) { @@ -39,23 +38,21 @@ public class UserInput extends QueryChain { this(null, annotation, value); } - public void setIndex(int index) { - placeholder = setDefaultIndex - ? "_" + index + "_" + indexField - : "_" + index; - } - @Override public String toString() { - //([{"defaultIndex": "shpdescfree"}](userInput(@_shpdescfree_1))) - return setDefaultIndex - ? Text.format("([%s]userInput(@%s))", annotation, placeholder) - : Text.format("userInput(@%s)", placeholder); - } - - - Map getParam() { - return Collections.singletonMap(placeholder, value); + StringBuilder b = new StringBuilder(); + if (setDefaultIndex) + b.append("(").append(annotation); + b.append("userInput("); + if ( ! valueIsReference) + b.append("\""); + b.append(value); + if ( ! valueIsReference) + b.append("\""); + b.append(")"); + if (setDefaultIndex) + b.append(")"); + return b.toString(); } @Override -- cgit v1.2.3