|
21 | 21 | import org.elasticsearch.xpack.esql.core.tree.Source; |
22 | 22 | import org.elasticsearch.xpack.esql.core.type.DataType; |
23 | 23 | import org.elasticsearch.xpack.esql.core.util.Check; |
| 24 | +import org.elasticsearch.xpack.esql.expression.function.Example; |
24 | 25 | import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesTo; |
25 | 26 | import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesToLifecycle; |
26 | 27 | import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; |
| 28 | +import org.elasticsearch.xpack.esql.expression.function.MapParam; |
27 | 29 | import org.elasticsearch.xpack.esql.expression.function.OptionalArgument; |
| 30 | +import org.elasticsearch.xpack.esql.expression.function.Param; |
28 | 31 | import org.elasticsearch.xpack.esql.expression.function.fulltext.FullTextFunction; |
29 | 32 | import org.elasticsearch.xpack.esql.expression.function.fulltext.Match; |
30 | 33 | import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput; |
@@ -66,11 +69,71 @@ public class Knn extends FullTextFunction implements OptionalArgument, VectorFun |
66 | 69 | entry(KnnQuery.RESCORE_OVERSAMPLE_FIELD, FLOAT) |
67 | 70 | ); |
68 | 71 |
|
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 | + ) { |
74 | 137 | this(source, field, query, options, null); |
75 | 138 | } |
76 | 139 |
|
|
0 commit comments