Skip to content

Commit bdfc1c2

Browse files
committed
Add testing
1 parent d25776a commit bdfc1c2

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

docs/reference/query-languages/esql/images/functions/knn.svg

Lines changed: 1 addition & 1 deletion
Loading

docs/reference/query-languages/esql/kibana/definition/functions/knn.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/query-languages/esql/kibana/docs/functions/knn.md

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/VerifierTests.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ public void testFieldBasedFullTextFunctions() throws Exception {
12361236
checkFieldBasedFunctionNotAllowedAfterCommands("Term", "function", "term(title, \"Meditation\")");
12371237
}
12381238
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1239-
checkFieldBasedFunctionNotAllowedAfterCommands("KNN", "function", "knn(vector, [1, 2, 3])");
1239+
checkFieldBasedFunctionNotAllowedAfterCommands("KNN", "function", "knn(vector, [1, 2, 3], 10)");
12401240
}
12411241
}
12421242

@@ -1369,7 +1369,7 @@ public void testFullTextFunctionsOnlyAllowedInWhere() throws Exception {
13691369
checkFullTextFunctionsOnlyAllowedInWhere("MultiMatch", "multi_match(\"Meditation\", title, body)", "function");
13701370
}
13711371
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1372-
checkFullTextFunctionsOnlyAllowedInWhere("KNN", "knn(vector, [0, 1, 2])", "function");
1372+
checkFullTextFunctionsOnlyAllowedInWhere("KNN", "knn(vector, [0, 1, 2], 10)", "function");
13731373
}
13741374
}
13751375

@@ -1408,7 +1408,7 @@ public void testFullTextFunctionsDisjunctions() {
14081408
checkWithFullTextFunctionsDisjunctions("term(title, \"Meditation\")");
14091409
}
14101410
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1411-
checkWithFullTextFunctionsDisjunctions("knn(vector, [1, 2, 3])");
1411+
checkWithFullTextFunctionsDisjunctions("knn(vector, [1, 2, 3], 10)");
14121412
}
14131413
}
14141414

@@ -1473,7 +1473,7 @@ public void testFullTextFunctionsWithNonBooleanFunctions() {
14731473
checkFullTextFunctionsWithNonBooleanFunctions("Term", "term(title, \"Meditation\")", "function");
14741474
}
14751475
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1476-
checkFullTextFunctionsWithNonBooleanFunctions("KNN", "knn(vector, [1, 2, 3])", "function");
1476+
checkFullTextFunctionsWithNonBooleanFunctions("KNN", "knn(vector, [1, 2, 3], 10)", "function");
14771477
}
14781478
}
14791479

@@ -1544,7 +1544,7 @@ public void testFullTextFunctionsTargetsExistingField() throws Exception {
15441544
testFullTextFunctionTargetsExistingField("term(fist_name, \"Meditation\")");
15451545
}
15461546
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1547-
testFullTextFunctionTargetsExistingField("knn(vector, [0, 1, 2])");
1547+
testFullTextFunctionTargetsExistingField("knn(vector, [0, 1, 2], 10)");
15481548
}
15491549
}
15501550

@@ -2072,7 +2072,7 @@ public void testFullTextFunctionOptions() {
20722072
checkOptionDataTypes(MultiMatch.OPTIONS, "FROM test | WHERE MULTI_MATCH(\"Jean\", title, body, {\"%s\": %s})");
20732073
}
20742074
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
2075-
checkOptionDataTypes(Knn.ALLOWED_OPTIONS, "FROM test | WHERE KNN(vector, [0.1, 0.2, 0.3], {\"%s\": %s})");
2075+
checkOptionDataTypes(Knn.ALLOWED_OPTIONS, "FROM test | WHERE KNN(vector, [0.1, 0.2, 0.3], 10, {\"%s\": %s})");
20762076
}
20772077
}
20782078

@@ -2160,8 +2160,9 @@ public void testFullTextFunctionsNullArgs() throws Exception {
21602160
checkFullTextFunctionNullArgs("term(title, null)", "second");
21612161
}
21622162
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
2163-
checkFullTextFunctionNullArgs("knn(null, [0, 1, 2])", "first");
2164-
checkFullTextFunctionNullArgs("knn(vector, null)", "second");
2163+
checkFullTextFunctionNullArgs("knn(null, [0, 1, 2], 10)", "first");
2164+
checkFullTextFunctionNullArgs("knn(vector, null, 10)", "second");
2165+
checkFullTextFunctionNullArgs("knn(vector, [0, 1, 2], null)", "third");
21652166
}
21662167
}
21672168

@@ -2185,7 +2186,7 @@ public void testFullTextFunctionsConstantQuery() throws Exception {
21852186
checkFullTextFunctionsConstantQuery("term(title, tags)", "second");
21862187
}
21872188
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
2188-
checkFullTextFunctionsConstantQuery("knn(vector, vector)", "second");
2189+
checkFullTextFunctionsConstantQuery("knn(vector, vector, 10)", "second");
21892190
}
21902191
}
21912192

@@ -2215,7 +2216,7 @@ public void testFullTextFunctionsInStats() {
22152216
checkFullTextFunctionsInStats("multi_match(\"Meditation\", title, body)");
22162217
}
22172218
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
2218-
checkFullTextFunctionsInStats("knn(vector, [0, 1, 2])");
2219+
checkFullTextFunctionsInStats("knn(vector, [0, 1, 2], 10)");
22192220
}
22202221
}
22212222

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/fulltext/KnnTests.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.xpack.esql.core.expression.MapExpression;
1919
import org.elasticsearch.xpack.esql.core.tree.Source;
2020
import org.elasticsearch.xpack.esql.core.type.DataType;
21+
import org.elasticsearch.xpack.esql.core.type.EsField;
2122
import org.elasticsearch.xpack.esql.expression.function.AbstractFunctionTestCase;
2223
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
2324
import org.elasticsearch.xpack.esql.expression.function.vector.Knn;
@@ -27,6 +28,7 @@
2728

2829
import java.util.ArrayList;
2930
import java.util.List;
31+
import java.util.Map;
3032
import java.util.function.Supplier;
3133

3234
import static org.elasticsearch.xpack.esql.SerializationTestUtils.serializeDeserialize;
@@ -56,12 +58,26 @@ private static List<TestCaseSupplier> testCaseSuppliers() {
5658
List<TestCaseSupplier> suppliers = new ArrayList<>();
5759

5860
suppliers.add(
59-
TestCaseSupplier.testCaseSupplier(
60-
new TestCaseSupplier.TypedDataSupplier("dense_vector field", KnnTests::randomDenseVector, DENSE_VECTOR),
61-
new TestCaseSupplier.TypedDataSupplier("query", KnnTests::randomDenseVector, DENSE_VECTOR, true),
62-
(d1, d2) -> equalTo("string"),
63-
BOOLEAN,
64-
(o1, o2) -> true
61+
new TestCaseSupplier(
62+
List.of(DENSE_VECTOR, DENSE_VECTOR, DataType.INTEGER),
63+
() -> new TestCaseSupplier.TestCase(
64+
List.of(
65+
new TestCaseSupplier.TypedData(
66+
new FieldAttribute(
67+
Source.EMPTY,
68+
randomIdentifier(),
69+
new EsField(randomIdentifier(), DENSE_VECTOR, Map.of(), false)
70+
),
71+
DENSE_VECTOR,
72+
"dense_vector field"
73+
),
74+
new TestCaseSupplier.TypedData(randomDenseVector(), DENSE_VECTOR, "query"),
75+
new TestCaseSupplier.TypedData(randomIntBetween(1, 1000), DataType.INTEGER, "k")
76+
),
77+
equalTo("KnnEvaluator" + KnnTests.class.getSimpleName()),
78+
BOOLEAN,
79+
equalTo(true)
80+
)
6581
)
6682
);
6783

@@ -104,7 +120,7 @@ private static List<TestCaseSupplier> addFunctionNamedParams(List<TestCaseSuppli
104120

105121
@Override
106122
protected Expression build(Source source, List<Expression> args) {
107-
Knn knn = new Knn(source, args.get(0), args.get(1), args.size() > 2 ? args.get(2) : null);
123+
Knn knn = new Knn(source, args.get(0), args.get(1), args.get(2), args.size() > 3 ? args.get(3) : null);
108124
// We need to add the QueryBuilder to the match expression, as it is used to implement equals() and hashCode() and
109125
// thus test the serialization methods. But we can only do this if the parameters make sense .
110126
if (args.get(0) instanceof FieldAttribute && args.get(1).foldable()) {

0 commit comments

Comments
 (0)