-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL brute force l1_norm vector function #131768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d3f68c2
39fd68e
84870ce
fdd1103
f3abd7b
b0a8410
af10ddf
99fd7ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Tests for l1_norm similarity function | ||
|
|
||
| similarityWithVectorField | ||
| required_capability: l1_norm_vector_similarity_function | ||
|
|
||
| // tag::vector-l1-norm-similarity[] | ||
| from colors | ||
| | eval similarity = v_l1_norm(rgb_vector, [0, 255, 255]) | ||
| | sort similarity desc, color asc | ||
| // end::vector-l1-norm-similarity[] | ||
| | limit 10 | ||
| | keep color, similarity | ||
| ; | ||
|
|
||
| // tag::vector-l1-norm-similarity-result[] | ||
| color:text | similarity:double | ||
| red | 765.0 | ||
| crimson | 650.0 | ||
| maroon | 638.0 | ||
| firebrick | 620.0 | ||
| orange | 600.0 | ||
| tomato | 595.0 | ||
| brown | 591.0 | ||
| chocolate | 585.0 | ||
| coral | 558.0 | ||
| gold | 550.0 | ||
| // end::vector-l1-norm-similarity-result[] | ||
| ; | ||
|
|
||
| similarityAsPartOfExpression | ||
| required_capability: l1_norm_vector_similarity_function | ||
|
|
||
| from colors | ||
| | eval score = round((1 + v_l1_norm(rgb_vector, [0, 255, 255]) / 2), 3) | ||
| | sort score desc, color asc | ||
| | limit 10 | ||
| | keep color, score | ||
| ; | ||
|
|
||
| color:text | score:double | ||
| red | 383.5 | ||
| crimson | 326.0 | ||
| maroon | 320.0 | ||
| firebrick | 311.0 | ||
| orange | 301.0 | ||
| tomato | 298.5 | ||
| brown | 296.5 | ||
| chocolate | 293.5 | ||
| coral | 280.0 | ||
| gold | 276.0 | ||
| ; | ||
|
|
||
| similarityWithLiteralVectors | ||
| required_capability: l1_norm_vector_similarity_function | ||
|
|
||
| row a = 1 | ||
| | eval similarity = round(v_l1_norm([1, 2, 3], [0, 1, 2]), 3) | ||
| | keep similarity | ||
| ; | ||
|
|
||
| similarity:double | ||
| 3.0 | ||
| ; | ||
|
|
||
| similarityWithStats | ||
| required_capability: l1_norm_vector_similarity_function | ||
|
|
||
| from colors | ||
| | eval similarity = round(v_l1_norm(rgb_vector, [0, 255, 255]), 3) | ||
| | stats avg = round(avg(similarity), 3), min = min(similarity), max = max(similarity) | ||
| ; | ||
|
|
||
| avg:double | min:double | max:double | ||
| 391.254 | 0.0 | 765.0 | ||
| ; | ||
|
|
||
| # TODO Need to implement a conversion function to convert a non-foldable row to a dense_vector | ||
| similarityWithRow-Ignore | ||
| required_capability: l1_norm_vector_similarity_function | ||
|
|
||
| row vector = [1, 2, 3] | ||
| | eval similarity = round(v_l1_norm(vector, [0, 1, 2]), 3) | ||
| | sort similarity desc, color asc | ||
| | limit 10 | ||
| | keep color, similarity | ||
| ; | ||
|
|
||
| similarity:double | ||
| 0.978 | ||
| ; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.expression.function.vector; | ||
|
|
||
| import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.expression.function.scalar.BinaryScalarFunction; | ||
| import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.expression.function.Example; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesTo; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesToLifecycle; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; | ||
| import org.elasticsearch.xpack.esql.expression.function.Param; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class L1Norm extends VectorSimilarityFunction { | ||
|
|
||
| public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "L1Norm", L1Norm::new); | ||
| static final SimilarityEvaluatorFunction SIMILARITY_FUNCTION = L1Norm::calculateSimilarity; | ||
|
|
||
| @FunctionInfo( | ||
| returnType = "double", | ||
| preview = true, | ||
| description = "Calculates the l1 norm between two dense_vectors.", | ||
| examples = { @Example(file = "vector-l1-norm", tag = "vector-l1-norm") }, | ||
| appliesTo = { @FunctionAppliesTo(lifeCycle = FunctionAppliesToLifecycle.DEVELOPMENT) } | ||
| ) | ||
| public L1Norm( | ||
| Source source, | ||
| @Param( | ||
| name = "left", | ||
| type = { "dense_vector" }, | ||
| description = "first dense_vector to calculate l1 norm similarity" | ||
| ) Expression left, | ||
| @Param( | ||
| name = "right", | ||
| type = { "dense_vector" }, | ||
| description = "second dense_vector to calculate l1 norm similarity" | ||
| ) Expression right | ||
| ) { | ||
| super(source, left, right); | ||
| } | ||
|
|
||
| private L1Norm(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
|
|
||
| @Override | ||
| protected BinaryScalarFunction replaceChildren(Expression newLeft, Expression newRight) { | ||
| return new L1Norm(source(), newLeft, newRight); | ||
| } | ||
|
|
||
| @Override | ||
| protected SimilarityEvaluatorFunction getSimilarityFunction() { | ||
| return SIMILARITY_FUNCTION; | ||
| } | ||
|
|
||
| @Override | ||
| protected NodeInfo<? extends Expression> info() { | ||
| return NodeInfo.create(this, L1Norm::new, left(), right()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getWriteableName() { | ||
| return ENTRY.name; | ||
| } | ||
|
|
||
| public static float calculateSimilarity(float[] leftScratch, float[] rightScratch) { | ||
| float result = 0f; | ||
| for (int i = 0; i < leftScratch.length; i++) { | ||
| result += Math.abs(leftScratch[i] - rightScratch[i]); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!