Skip to content

Commit 315aba6

Browse files
authored
ESQL - Add K mandatory param for KNN function (#129763)
1 parent a9625ce commit 315aba6

File tree

15 files changed

+194
-138
lines changed

15 files changed

+194
-138
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: 1 addition & 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.

muted-tests.yml

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ tests:
236236
- class: org.elasticsearch.packaging.test.DockerTests
237237
method: test012SecurityCanBeDisabled
238238
issue: https://github.com/elastic/elasticsearch/issues/116636
239+
- class: org.elasticsearch.index.shard.StoreRecoveryTests
240+
method: testAddIndices
241+
issue: https://github.com/elastic/elasticsearch/issues/124104
239242
- class: org.elasticsearch.smoketest.MlWithSecurityIT
240243
method: test {yaml=ml/data_frame_analytics_crud/Test get stats on newly created config}
241244
issue: https://github.com/elastic/elasticsearch/issues/121726
@@ -455,6 +458,12 @@ tests:
455458
- class: org.elasticsearch.packaging.test.DockerTests
456459
method: test073RunEsAsDifferentUserAndGroupWithoutBindMounting
457460
issue: https://github.com/elastic/elasticsearch/issues/128996
461+
- class: org.elasticsearch.upgrades.UpgradeClusterClientYamlTestSuiteIT
462+
method: test {p0=upgraded_cluster/70_ilm/Test Lifecycle Still There And Indices Are Still Managed}
463+
issue: https://github.com/elastic/elasticsearch/issues/129097
464+
- class: org.elasticsearch.upgrades.UpgradeClusterClientYamlTestSuiteIT
465+
method: test {p0=upgraded_cluster/90_ml_data_frame_analytics_crud/Get mixed cluster outlier_detection job}
466+
issue: https://github.com/elastic/elasticsearch/issues/129098
458467
- class: org.elasticsearch.packaging.test.DockerTests
459468
method: test081SymlinksAreFollowedWithEnvironmentVariableFiles
460469
issue: https://github.com/elastic/elasticsearch/issues/128867
@@ -473,21 +482,27 @@ tests:
473482
- class: org.elasticsearch.entitlement.runtime.policy.FileAccessTreeTests
474483
method: testWindowsAbsolutPathAccess
475484
issue: https://github.com/elastic/elasticsearch/issues/129168
476-
- class: org.elasticsearch.xpack.esql.qa.multi_node.EsqlSpecIT
477-
method: test {knn-function.KnnSearchWithKOption ASYNC}
478-
issue: https://github.com/elastic/elasticsearch/issues/129447
479485
- class: org.elasticsearch.xpack.ml.integration.ClassificationIT
480486
method: testWithDatastreams
481487
issue: https://github.com/elastic/elasticsearch/issues/129457
482488
- class: org.elasticsearch.index.engine.ThreadPoolMergeExecutorServiceDiskSpaceTests
483489
method: testMergeTasksAreUnblockedWhenMoreDiskSpaceBecomesAvailable
484490
issue: https://github.com/elastic/elasticsearch/issues/129296
491+
- class: org.elasticsearch.xpack.security.PermissionsIT
492+
method: testCanManageIndexWithNoPermissions
493+
issue: https://github.com/elastic/elasticsearch/issues/129471
494+
- class: org.elasticsearch.xpack.security.PermissionsIT
495+
method: testCanManageIndexAndPolicyDifferentUsers
496+
issue: https://github.com/elastic/elasticsearch/issues/129479
497+
- class: org.elasticsearch.xpack.security.PermissionsIT
498+
method: testCanViewExplainOnUnmanagedIndex
499+
issue: https://github.com/elastic/elasticsearch/issues/129480
485500
- class: org.elasticsearch.xpack.profiling.action.GetStatusActionIT
486501
method: testWaitsUntilResourcesAreCreated
487502
issue: https://github.com/elastic/elasticsearch/issues/129486
488-
- class: org.elasticsearch.xpack.esql.qa.multi_node.EsqlSpecIT
489-
method: test {knn-function.KnnSearchWithKOption SYNC}
490-
issue: https://github.com/elastic/elasticsearch/issues/129512
503+
- class: org.elasticsearch.xpack.security.PermissionsIT
504+
method: testWhenUserLimitedByOnlyAliasOfIndexCanWriteToIndexWhichWasRolledoverByILMPolicy
505+
issue: https://github.com/elastic/elasticsearch/issues/129481
491506
- class: org.elasticsearch.index.engine.ThreadPoolMergeExecutorServiceTests
492507
method: testIORateIsAdjustedForAllRunningMergeTasks
493508
issue: https://github.com/elastic/elasticsearch/issues/129531
@@ -503,15 +518,24 @@ tests:
503518
- class: org.elasticsearch.search.query.VectorIT
504519
method: testFilteredQueryStrategy
505520
issue: https://github.com/elastic/elasticsearch/issues/129517
521+
- class: org.elasticsearch.snapshots.SnapshotShutdownIT
522+
method: testSnapshotShutdownProgressTracker
523+
issue: https://github.com/elastic/elasticsearch/issues/129752
506524
- class: org.elasticsearch.xpack.security.SecurityRolesMultiProjectIT
507525
method: testUpdatingFileBasedRoleAffectsAllProjects
508526
issue: https://github.com/elastic/elasticsearch/issues/129775
509527
- class: org.elasticsearch.qa.verify_version_constants.VerifyVersionConstantsIT
510528
method: testLuceneVersionConstant
511529
issue: https://github.com/elastic/elasticsearch/issues/125638
530+
- class: org.elasticsearch.index.store.FsDirectoryFactoryTests
531+
method: testPreload
532+
issue: https://github.com/elastic/elasticsearch/issues/129852
512533
- class: org.elasticsearch.xpack.rank.rrf.RRFRankClientYamlTestSuiteIT
513534
method: test {yaml=rrf/950_pinned_interaction/rrf with pinned retriever as a sub-retriever}
514535
issue: https://github.com/elastic/elasticsearch/issues/129845
536+
- class: org.elasticsearch.xpack.test.rest.XPackRestIT
537+
method: test {p0=esql/60_usage/Basic ESQL usage output (telemetry) non-snapshot version}
538+
issue: https://github.com/elastic/elasticsearch/issues/129888
515539
- class: org.elasticsearch.gradle.internal.InternalDistributionBwcSetupPluginFuncTest
516540
method: "builds distribution from branches via archives extractedAssemble [bwcDistVersion: 8.2.1, bwcProject: bugfix, expectedAssembleTaskName:
517541
extractedAssemble, #2]"
@@ -525,9 +549,14 @@ tests:
525549
- class: org.elasticsearch.xpack.esql.qa.multi_node.GenerativeIT
526550
method: test
527551
issue: https://github.com/elastic/elasticsearch/issues/130067
552+
- class: geoip.GeoIpMultiProjectIT
553+
issue: https://github.com/elastic/elasticsearch/issues/130073
528554
- class: org.elasticsearch.xpack.esql.qa.single_node.GenerativeIT
529555
method: test
530556
issue: https://github.com/elastic/elasticsearch/issues/130067
557+
- class: org.elasticsearch.xpack.esql.action.EnrichIT
558+
method: testTopN
559+
issue: https://github.com/elastic/elasticsearch/issues/130122
531560
- class: org.elasticsearch.action.support.ThreadedActionListenerTests
532561
method: testRejectionHandling
533562
issue: https://github.com/elastic/elasticsearch/issues/130129

x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# top-n query at the shard level
44

55
knnSearch
6-
required_capability: knn_function
6+
required_capability: knn_function_v2
77

88
// tag::knn-function[]
99
from colors metadata _score
10-
| where knn(rgb_vector, [0, 120, 0])
10+
| where knn(rgb_vector, [0, 120, 0], 10)
1111
| sort _score desc, color asc
1212
// end::knn-function[]
1313
| keep color, rgb_vector
@@ -29,31 +29,12 @@ chartreuse | [127.0, 255.0, 0.0]
2929
// end::knn-function-result[]
3030
;
3131

32-
knnSearchWithKOption
33-
required_capability: knn_function
34-
35-
// tag::knn-function-options[]
36-
from colors metadata _score
37-
| where knn(rgb_vector, [0,255,255], {"k": 4})
38-
| sort _score desc, color asc
39-
// end::knn-function-options[]
40-
| keep color, rgb_vector
41-
| limit 4
42-
;
43-
44-
color:text | rgb_vector:dense_vector
45-
cyan | [0.0, 255.0, 255.0]
46-
turquoise | [64.0, 224.0, 208.0]
47-
aqua marine | [127.0, 255.0, 212.0]
48-
teal | [0.0, 128.0, 128.0]
49-
;
50-
51-
# https://github.com/elastic/elasticsearch/issues/129550
32+
# https://github.com/elastic/elasticsearch/issues/129550 - Add as an example to knn function documentation
5233
knnSearchWithSimilarityOption-Ignore
53-
required_capability: knn_function
34+
required_capability: knn_function_v2
5435

5536
from colors metadata _score
56-
| where knn(rgb_vector, [255,192,203], {"k": 140, "similarity": 40})
37+
| where knn(rgb_vector, [255,192,203], 140, {"similarity": 40})
5738
| sort _score desc, color asc
5839
| keep color, rgb_vector
5940
;
@@ -63,14 +44,13 @@ pink | [255.0, 192.0, 203.0]
6344
peach puff | [255.0, 218.0, 185.0]
6445
bisque | [255.0, 228.0, 196.0]
6546
wheat | [245.0, 222.0, 179.0]
66-
6747
;
6848

6949
knnHybridSearch
70-
required_capability: knn_function
50+
required_capability: knn_function_v2
7151

7252
from colors metadata _score
73-
| where match(color, "blue") or knn(rgb_vector, [65,105,225], {"k": 140})
53+
| where match(color, "blue") or knn(rgb_vector, [65,105,225], 140)
7454
| where primary == true
7555
| sort _score desc, color asc
7656
| keep color, rgb_vector
@@ -90,10 +70,10 @@ yellow | [255.0, 255.0, 0.0]
9070
;
9171

9272
knnWithMultipleFunctions
93-
required_capability: knn_function
73+
required_capability: knn_function_v2
9474

9575
from colors metadata _score
96-
| where knn(rgb_vector, [128,128,0], {"k": 140}) and match(color, "olive")
76+
| where knn(rgb_vector, [128,128,0], 140) and match(color, "olive")
9777
| sort _score desc, color asc
9878
| keep color, rgb_vector
9979
;
@@ -103,11 +83,11 @@ olive | [128.0, 128.0, 0.0]
10383
;
10484

10585
knnAfterKeep
106-
required_capability: knn_function
86+
required_capability: knn_function_v2
10787

10888
from colors metadata _score
10989
| keep rgb_vector, color, _score
110-
| where knn(rgb_vector, [128,255,0], {"k": 140})
90+
| where knn(rgb_vector, [128,255,0], 140)
11191
| sort _score desc, color asc
11292
| keep rgb_vector
11393
| limit 5
@@ -122,11 +102,11 @@ rgb_vector:dense_vector
122102
;
123103

124104
knnAfterDrop
125-
required_capability: knn_function
105+
required_capability: knn_function_v2
126106

127107
from colors metadata _score
128108
| drop primary
129-
| where knn(rgb_vector, [128,250,0], {"k": 140})
109+
| where knn(rgb_vector, [128,250,0], 140)
130110
| sort _score desc, color asc
131111
| keep color, rgb_vector
132112
| limit 5
@@ -141,11 +121,11 @@ lime | [0.0, 255.0, 0.0]
141121
;
142122

143123
knnAfterEval
144-
required_capability: knn_function
124+
required_capability: knn_function_v2
145125

146126
from colors metadata _score
147127
| eval composed_name = locate(color, " ") > 0
148-
| where knn(rgb_vector, [128,128,0], {"k": 140})
128+
| where knn(rgb_vector, [128,128,0], 140)
149129
| sort _score desc, color asc
150130
| keep color, composed_name
151131
| limit 5
@@ -160,11 +140,11 @@ golden rod | true
160140
;
161141

162142
knnWithConjunction
163-
required_capability: knn_function
143+
required_capability: knn_function_v2
164144

165145
# TODO We need kNN prefiltering here so we get more candidates that pass the filter
166146
from colors metadata _score
167-
| where knn(rgb_vector, [255,255,238], {"k": 140}) and hex_code like "#FFF*"
147+
| where knn(rgb_vector, [255,255,238], 140) and hex_code like "#FFF*"
168148
| sort _score desc, color asc
169149
| keep color, hex_code, rgb_vector
170150
| limit 10
@@ -181,11 +161,11 @@ yellow | #FFFF00 | [255.0, 255.0, 0.0]
181161
;
182162

183163
knnWithDisjunctionAndFiltersConjunction
184-
required_capability: knn_function
164+
required_capability: knn_function_v2
185165

186166
# TODO We need kNN prefiltering here so we get more candidates that pass the filter
187167
from colors metadata _score
188-
| where (knn(rgb_vector, [0,255,255], {"k": 140}) or knn(rgb_vector, [128, 0, 255], {"k": 140})) and primary == true
168+
| where (knn(rgb_vector, [0,255,255], 140) or knn(rgb_vector, [128, 0, 255], 140)) and primary == true
189169
| keep color, rgb_vector, _score
190170
| sort _score desc, color asc
191171
| drop _score
@@ -205,11 +185,11 @@ yellow | [255.0, 255.0, 0.0]
205185
;
206186

207187
knnWithNonPushableConjunction
208-
required_capability: knn_function
188+
required_capability: knn_function_v2
209189

210190
from colors metadata _score
211191
| eval composed_name = locate(color, " ") > 0
212-
| where knn(rgb_vector, [128,128,0], {"k": 140}) and composed_name == false
192+
| where knn(rgb_vector, [128,128,0], 140) and composed_name == false
213193
| sort _score desc, color asc
214194
| keep color, composed_name
215195
| limit 10
@@ -230,10 +210,10 @@ maroon | false
230210

231211
# https://github.com/elastic/elasticsearch/issues/129550
232212
testKnnWithNonPushableDisjunctions-Ignore
233-
required_capability: knn_function
213+
required_capability: knn_function_v2
234214

235215
from colors metadata _score
236-
| where knn(rgb_vector, [128,128,0], {"k": 140, "similarity": 30}) or length(color) > 10
216+
| where knn(rgb_vector, [128,128,0], 140, {"similarity": 30}) or length(color) > 10
237217
| sort _score desc, color asc
238218
| keep color
239219
;
@@ -247,10 +227,10 @@ papaya whip
247227

248228
# https://github.com/elastic/elasticsearch/issues/129550
249229
testKnnWithNonPushableDisjunctionsOnComplexExpressions-Ignore
250-
required_capability: knn_function
230+
required_capability: knn_function_v2
251231

252232
from colors metadata _score
253-
| where (knn(rgb_vector, [128,128,0], {"k": 140, "similarity": 70}) and length(color) < 10) or (knn(rgb_vector, [128,0,128], {"k": 140, "similarity": 60}) and primary == false)
233+
| where (knn(rgb_vector, [128,128,0], 140, {"similarity": 70}) and length(color) < 10) or (knn(rgb_vector, [128,0,128], 140, {"similarity": 60}) and primary == false)
254234
| sort _score desc, color asc
255235
| keep color, primary
256236
;
@@ -262,24 +242,24 @@ indigo | false
262242
;
263243

264244
testKnnInStatsNonPushable
265-
required_capability: knn_function
245+
required_capability: knn_function_v2
266246

267247
from colors
268248
| where length(color) < 10
269-
| stats c = count(*) where knn(rgb_vector, [128,128,255], {"k": 140})
249+
| stats c = count(*) where knn(rgb_vector, [128,128,255], 140)
270250
;
271251

272252
c: long
273253
50
274254
;
275255

276256
testKnnInStatsWithGrouping
277-
required_capability: knn_function
257+
required_capability: knn_function_v2
278258
required_capability: full_text_functions_in_stats_where
279259

280260
from colors
281261
| where length(color) < 10
282-
| stats c = count(*) where knn(rgb_vector, [128,128,255], {"k": 140}) by primary
262+
| stats c = count(*) where knn(rgb_vector, [128,128,255], 140) by primary
283263
;
284264

285265
c: long | primary: boolean

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/KnnFunctionIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testKnnDefaults() {
3939

4040
var query = String.format(Locale.ROOT, """
4141
FROM test METADATA _score
42-
| WHERE knn(vector, %s)
42+
| WHERE knn(vector, %s, 10)
4343
| KEEP id, floats, _score, vector
4444
| SORT _score DESC
4545
""", Arrays.toString(queryVector));
@@ -73,7 +73,7 @@ public void testKnnOptions() {
7373

7474
var query = String.format(Locale.ROOT, """
7575
FROM test METADATA _score
76-
| WHERE knn(vector, %s, {"k": 5})
76+
| WHERE knn(vector, %s, 5)
7777
| KEEP id, floats, _score, vector
7878
| SORT _score DESC
7979
""", Arrays.toString(queryVector));
@@ -94,7 +94,7 @@ public void testKnnNonPushedDown() {
9494
// TODO we need to decide what to do when / if user uses k for limit, as no more than k results will be returned from knn query
9595
var query = String.format(Locale.ROOT, """
9696
FROM test METADATA _score
97-
| WHERE knn(vector, %s, {"k": 5}) OR id > 10
97+
| WHERE knn(vector, %s, 5) OR id > 10
9898
| KEEP id, floats, _score, vector
9999
| SORT _score DESC
100100
""", Arrays.toString(queryVector));
@@ -111,7 +111,7 @@ public void testKnnNonPushedDown() {
111111

112112
@Before
113113
public void setup() throws IOException {
114-
assumeTrue("Needs KNN support", EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled());
114+
assumeTrue("Needs KNN support", EsqlCapabilities.Cap.KNN_FUNCTION_V2.isEnabled());
115115

116116
var indexName = "test";
117117
var client = client().admin().indices();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ public enum Cap {
11951195
/**
11961196
* Support knn function
11971197
*/
1198-
KNN_FUNCTION(Build.current().isSnapshot()),
1198+
KNN_FUNCTION_V2(Build.current().isSnapshot()),
11991199

12001200
LIKE_WITH_LIST_OF_PATTERNS,
12011201

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/ExpressionWritables.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private static List<NamedWriteableRegistry.Entry> fullText() {
259259
}
260260

261261
private static List<NamedWriteableRegistry.Entry> vector() {
262-
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
262+
if (EsqlCapabilities.Cap.KNN_FUNCTION_V2.isEnabled()) {
263263
return List.of(Knn.ENTRY);
264264
}
265265
return List.of();

0 commit comments

Comments
 (0)