aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2023-07-09 23:10:43 +0200
committerGitHub <noreply@github.com>2023-07-09 23:10:43 +0200
commit7a7ee8fe926e14c69f4ea2794c14414511101bbf (patch)
treebbea646c92a1e3d3a092163f0e0b42d47c39c2b2
parent3168abdd33e16054275719f1c33f3fd474413eac (diff)
parent6cc3c92a70ed43c2e57439e085da8de50893adc0 (diff)
Merge pull request #27718 from vespa-engine/bratseth/fuzzyv8.191.40
Add fuzzy
-rw-r--r--client/src/main/java/ai/vespa/client/dsl/Field.java25
-rw-r--r--client/src/main/java/ai/vespa/client/dsl/Q.java2
-rw-r--r--client/src/test/java/ai/vespa/client/dsl/QTest.java12
3 files changed, 38 insertions, 1 deletions
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
@@ -485,6 +485,18 @@ class QTest {
}
@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"))
.build();