Skip to content

Commit 22efe27

Browse files
committed
Add Knn doc annotations
1 parent e7452dd commit 22efe27

File tree

2 files changed

+102
-5
lines changed
  • x-pack/plugin/esql/src
    • main/java/org/elasticsearch/xpack/esql/expression/function/vector
    • test/java/org/elasticsearch/xpack/esql/expression/function/fulltext

2 files changed

+102
-5
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/vector/Knn.java

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import org.elasticsearch.xpack.esql.core.tree.Source;
2222
import org.elasticsearch.xpack.esql.core.type.DataType;
2323
import org.elasticsearch.xpack.esql.core.util.Check;
24+
import org.elasticsearch.xpack.esql.expression.function.Example;
2425
import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesTo;
2526
import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesToLifecycle;
2627
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
28+
import org.elasticsearch.xpack.esql.expression.function.MapParam;
2729
import org.elasticsearch.xpack.esql.expression.function.OptionalArgument;
30+
import org.elasticsearch.xpack.esql.expression.function.Param;
2831
import org.elasticsearch.xpack.esql.expression.function.fulltext.FullTextFunction;
2932
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
3033
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
@@ -66,11 +69,71 @@ public class Knn extends FullTextFunction implements OptionalArgument, VectorFun
6669
entry(KnnQuery.RESCORE_OVERSAMPLE_FIELD, FLOAT)
6770
);
6871

69-
@FunctionInfo(returnType = "boolean", preview = true, description = """
70-
Finds the k nearest vectors to a query vector, as measured by a similarity metric.
71-
knn function finds nearest vectors through approximate search on indexed dense_vectors
72-
""", appliesTo = { @FunctionAppliesTo(lifeCycle = FunctionAppliesToLifecycle.DEVELOPMENT) })
73-
public Knn(Source source, Expression field, Expression query, Expression options) {
72+
@FunctionInfo(
73+
returnType = "boolean",
74+
preview = true,
75+
description = """
76+
Finds the k nearest vectors to a query vector, as measured by a similarity metric.
77+
knn function finds nearest vectors through approximate search on indexed dense_vectors
78+
""",
79+
examples = {
80+
@Example(file = "knn-function", tag = "knn-function"),
81+
@Example(file = "knn-function", tag = "knn-function-options"), },
82+
appliesTo = { @FunctionAppliesTo(lifeCycle = FunctionAppliesToLifecycle.DEVELOPMENT) }
83+
)
84+
public Knn(
85+
Source source,
86+
@Param(name = "field", type = { "dense_vector" }, description = "Field that the query will target.") Expression field,
87+
@Param(
88+
name = "query",
89+
type = { "dense_vector" },
90+
description = "Vector value to find top nearest neighbours for."
91+
) Expression query,
92+
@MapParam(
93+
name = "options",
94+
params = {
95+
@MapParam.MapParamEntry(
96+
name = "boost",
97+
type = "float",
98+
valueHint = { "2.5" },
99+
description = "Floating point number used to decrease or increase the relevance scores of the query. "
100+
+ "Defaults to 1.0."
101+
),
102+
@MapParam.MapParamEntry(
103+
name = "k",
104+
type = "integer",
105+
valueHint = { "10" },
106+
description = "The number of nearest neighbors to return from each shard. "
107+
+ "Elasticsearch collects k results from each shard, then merges them to find the global top results. "
108+
+ "This value must be less than or equal to num_candidates. Defaults to 10."
109+
),
110+
@MapParam.MapParamEntry(
111+
name = "num_candidates",
112+
type = "integer",
113+
valueHint = { "10" },
114+
description = "The number of nearest neighbor candidates to consider per shard while doing knn search. "
115+
+ "Cannot exceed 10,000. Increasing num_candidates tends to improve the accuracy of the final results. "
116+
+ "Defaults to 1.5 * k"
117+
),
118+
@MapParam.MapParamEntry(
119+
name = "similarity",
120+
type = "double",
121+
valueHint = { "0.01" },
122+
description = "The minimum similarity required for a document to be considered a match. "
123+
+ "The similarity value calculated relates to the raw similarity used, not the document score"
124+
),
125+
@MapParam.MapParamEntry(
126+
name = "rescore_oversample",
127+
type = "double",
128+
valueHint = { "3.5" },
129+
description = "Applies the specified oversampling for rescoring quantized vectors. "
130+
+ "See [oversampling and rescoring quantized vectors](docs-content://solutions/search/vector/knn.md#dense-vector-knn-search-rescoring) for details."
131+
), },
132+
description = "(Optional) kNN additional options as <<esql-function-named-params,function named parameters>>."
133+
+ " See <<query-dsl-knn-query,knn query>> for more information.",
134+
optional = true
135+
) Expression options
136+
) {
74137
this(source, field, query, options, null);
75138
}
76139

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.fulltext;
9+
10+
import com.carrotsearch.randomizedtesting.annotations.Name;
11+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
12+
13+
import org.elasticsearch.xpack.esql.core.expression.Expression;
14+
import org.elasticsearch.xpack.esql.core.tree.Source;
15+
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
16+
17+
import java.util.List;
18+
import java.util.function.Supplier;
19+
20+
public class KnnTests extends NoneFieldFullTextFunctionTestCase {
21+
public KnnTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
22+
super(testCaseSupplier);
23+
}
24+
25+
@ParametersFactory
26+
public static Iterable<Object[]> parameters() {
27+
return generateParameters();
28+
}
29+
30+
@Override
31+
protected Expression build(Source source, List<Expression> args) {
32+
return new Kql(source, args.get(0));
33+
}
34+
}

0 commit comments

Comments
 (0)