From 6cc3c92a70ed43c2e57439e085da8de50893adc0 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Sun, 9 Jul 2023 22:47:57 +0200 Subject: Add fuzzy --- .../src/main/java/ai/vespa/client/dsl/Field.java | 25 ++++++++++++++++++++++ client/src/main/java/ai/vespa/client/dsl/Q.java | 2 +- .../src/test/java/ai/vespa/client/dsl/QTest.java | 12 +++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/client/src/main/java/ai/vespa/client/dsl/Field.java b/client/src/main/java/ai/vespa/client/dsl/Field.java index 6d199ead2b8..59459899189 100644 --- a/client/src/main/java/ai/vespa/client/dsl/Field.java +++ b/client/src/main/java/ai/vespa/client/dsl/Field.java @@ -571,6 +571,29 @@ public class Field extends QueryChain { return common("nearestNeighbor", annotation, (Object) rankFeature); } + /** + * Fuzzy query. + * https://docs.vespa.ai/en/reference/query-language-reference.html#fuzzy + * + * @param text the text to match fuzzily + * @return the query + */ + public Query fuzzy(String text) { + return common("fuzzy", annotation, text); + } + + /** + * Fuzzy query. + * https://docs.vespa.ai/en/reference/query-language-reference.html#fuzzy + * + * @param annotation the annotation + * @param text the text to match fuzzily + * @return the query + */ + public Query fuzzy(Annotation annotation, String text) { + return common("fuzzy", annotation, text); + } + private Query common(String relation, Annotation annotation, Object value) { return common(relation, annotation, value, values.toArray()); } @@ -629,6 +652,8 @@ public class Field extends QueryChain { return hasAnnotation ? Text.format("([%s]nearestNeighbor(%s, %s))", annotation, fieldName, valuesStr) : Text.format("nearestNeighbor(%s, %s)", fieldName, valuesStr); + case "fuzzy": + return Text.format("%s contains (%sfuzzy(%s))", fieldName, annotation, values.get(0)); default: Object value = values.get(0); valuesStr = value instanceof Long ? value + "L" : value.toString(); 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 2bb998cd3e5..e4cfd4aa1ef 100644 --- a/client/src/main/java/ai/vespa/client/dsl/Q.java +++ b/client/src/main/java/ai/vespa/client/dsl/Q.java @@ -22,7 +22,7 @@ public final class Q { throw new RuntimeException(e); } } - private static Sources SELECT_ALL_FROM_SOURCES_ALL = new Sources(new Select("*"), "*"); + private static final Sources SELECT_ALL_FROM_SOURCES_ALL = new Sources(new Select("*"), "*"); public static Select select(String fieldName) { return new Select(fieldName); } 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 aae8b2c8923..c242349873c 100644 --- a/client/src/test/java/ai/vespa/client/dsl/QTest.java +++ b/client/src/test/java/ai/vespa/client/dsl/QTest.java @@ -484,6 +484,18 @@ class QTest { assertEquals(q, "yql=select * from sources * where ([{\"targetHits\":10}]nearestNeighbor(f1, query_vector))"); } + @Test + void fuzzy() { + String q = Q.p("f1").fuzzy("text to match").build(); + assertEquals("yql=select * from sources * where f1 contains (fuzzy(\"text to match\"))", q); + } + + @Test + void fuzzy_with_annotation() { + String q = Q.p("f1").fuzzy(A.a("maxEditDistance", 3).append(A.a("prefixLength", 10)), "text to match").build(); + assertEquals("yql=select * from sources * where f1 contains ({\"prefixLength\":10,\"maxEditDistance\":3}fuzzy(\"text to match\"))", q); + } + @Test void use_contains_instead_of_contains_equiv_when_input_size_is_1() { String q = Q.p("f1").containsEquiv(Collections.singletonList("p1")) -- cgit v1.2.3