|
| 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.vector; |
| 9 | + |
| 10 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 11 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 12 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 13 | +import org.elasticsearch.xpack.esql.capabilities.TranslationAware; |
| 14 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 15 | +import org.elasticsearch.xpack.esql.core.expression.FoldContext; |
| 16 | +import org.elasticsearch.xpack.esql.core.expression.TypeResolutions; |
| 17 | +import org.elasticsearch.xpack.esql.core.expression.function.Function; |
| 18 | +import org.elasticsearch.xpack.esql.core.querydsl.query.Query; |
| 19 | +import org.elasticsearch.xpack.esql.core.tree.NodeInfo; |
| 20 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 21 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 22 | +import org.elasticsearch.xpack.esql.core.util.Check; |
| 23 | +import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesTo; |
| 24 | +import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesToLifecycle; |
| 25 | +import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; |
| 26 | +import org.elasticsearch.xpack.esql.expression.function.fulltext.Match; |
| 27 | +import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput; |
| 28 | +import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates; |
| 29 | +import org.elasticsearch.xpack.esql.planner.TranslatorHandler; |
| 30 | +import org.elasticsearch.xpack.esql.querydsl.query.KnnQuery; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST; |
| 37 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNull; |
| 38 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType; |
| 39 | +import static org.elasticsearch.xpack.esql.core.type.DataType.DENSE_VECTOR; |
| 40 | +import static org.elasticsearch.xpack.esql.expression.function.fulltext.Match.getNameFromFieldAttribute; |
| 41 | + |
| 42 | +public class Knn extends Function implements TranslationAware { |
| 43 | + |
| 44 | + public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Knn", Knn::readFrom); |
| 45 | + |
| 46 | + private final Expression field; |
| 47 | + private final Expression query; |
| 48 | + |
| 49 | + @FunctionInfo( |
| 50 | + returnType = "boolean", |
| 51 | + preview = true, |
| 52 | + description = """ |
| 53 | + Finds the k nearest vectors to a query vector, as measured by a similarity metric. |
| 54 | + knn function finds nearest vectors through approximate search on indexed dense_vectors |
| 55 | + """, |
| 56 | + appliesTo = { |
| 57 | + @FunctionAppliesTo( |
| 58 | + lifeCycle = FunctionAppliesToLifecycle.DEVELOPMENT |
| 59 | + ) } |
| 60 | + ) |
| 61 | + public Knn(Source source, Expression field, Expression query) { |
| 62 | + super(source, List.of(field, query)); |
| 63 | + this.field = field; |
| 64 | + this.query = query; |
| 65 | + } |
| 66 | + |
| 67 | + public Expression field() { |
| 68 | + return field; |
| 69 | + } |
| 70 | + |
| 71 | + public Expression query() { |
| 72 | + return query; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public DataType dataType() { |
| 77 | + return DataType.BOOLEAN; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected final TypeResolution resolveType() { |
| 82 | + if (childrenResolved() == false) { |
| 83 | + return new TypeResolution("Unresolved children"); |
| 84 | + } |
| 85 | + |
| 86 | + return isNotNull(field(), sourceText(), FIRST).and(isType(field(), dt -> dt == DENSE_VECTOR, sourceText(), FIRST, "dense_vector")) |
| 87 | + .and(TypeResolutions.isNumeric(query(), sourceText(), TypeResolutions.ParamOrdinal.SECOND)); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean translatable(LucenePushdownPredicates pushdownPredicates) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) { |
| 97 | + var fieldAttribute = Match.fieldAsFieldAttribute(field()); |
| 98 | + |
| 99 | + Check.notNull(fieldAttribute, "Match must have a field attribute as the first argument"); |
| 100 | + String fieldName = getNameFromFieldAttribute(fieldAttribute); |
| 101 | + @SuppressWarnings("unchecked") |
| 102 | + List<Double> queryFolded = (List<Double>) query().fold(FoldContext.small() /* TODO remove me */); |
| 103 | + float[] queryAsFloats = new float[queryFolded.size()]; |
| 104 | + for (int i = 0; i < queryFolded.size(); i++) { |
| 105 | + queryAsFloats[i] = queryFolded.get(i).floatValue(); |
| 106 | + } |
| 107 | + return new KnnQuery(source(), fieldName, queryAsFloats); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Expression replaceChildren(List<Expression> newChildren) { |
| 112 | + return new Knn(source(), newChildren.get(0), newChildren.get(1)); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected NodeInfo<? extends Expression> info() { |
| 117 | + return NodeInfo.create(this, Knn::new, field(), query()); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String getWriteableName() { |
| 122 | + return ENTRY.name; |
| 123 | + } |
| 124 | + |
| 125 | + private static Knn readFrom(StreamInput in) throws IOException { |
| 126 | + Source source = Source.readFrom((PlanStreamInput) in); |
| 127 | + Expression field = in.readNamedWriteable(Expression.class); |
| 128 | + Expression query = in.readNamedWriteable(Expression.class); |
| 129 | + |
| 130 | + return new Knn(source, field, query); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void writeTo(StreamOutput out) throws IOException { |
| 135 | + source().writeTo(out); |
| 136 | + out.writeNamedWriteable(field()); |
| 137 | + out.writeNamedWriteable(query()); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public boolean equals(Object o) { |
| 142 | + if (o == null || getClass() != o.getClass()) return false; |
| 143 | + if (super.equals(o) == false) return false; |
| 144 | + Knn knn = (Knn) o; |
| 145 | + return Objects.equals(field, knn.field) && Objects.equals(query, knn.query); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int hashCode() { |
| 150 | + return Objects.hash(super.hashCode(), field, query); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments