-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d3f68c2
ES|QL brute force l1_norm vector function
tteofili 39fd68e
Merge branch 'main' of github.com:elastic/elasticsearch into esql_dvb…
tteofili 84870ce
spotless
tteofili fdd1103
test fix
tteofili f3abd7b
Merge branch 'main' into esql_dvbf_l1norm
tteofili b0a8410
Merge branch 'main' into esql_dvbf_l1norm
tteofili af10ddf
Merge branch 'main' of github.com:elastic/elasticsearch into esql_dvb…
tteofili 99fd7ef
added missing docs
tteofili File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
docs/reference/query-languages/esql/images/functions/v_l1_norm.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
docs/reference/query-languages/esql/kibana/definition/functions/v_l1_norm.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
docs/reference/query-languages/esql/kibana/docs/functions/v_l1_norm.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
x-pack/plugin/esql/qa/testFixtures/src/main/resources/vector-l1-norm.csv-spec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.5 | 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 | ||
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...in/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/vector/L1Norm.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...t/java/org/elasticsearch/xpack/esql/expression/function/vector/L1NormSimilarityTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * 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 com.carrotsearch.randomizedtesting.annotations.Name; | ||
| import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
|
||
| import org.elasticsearch.xpack.esql.action.EsqlCapabilities; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionName; | ||
| import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; | ||
|
|
||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
|
|
||
| @FunctionName("v_l1_norm") | ||
| public class L1NormSimilarityTests extends AbstractVectorSimilarityFunctionTestCase { | ||
|
|
||
| public L1NormSimilarityTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) { | ||
| super(testCaseSupplier); | ||
| } | ||
|
|
||
| @ParametersFactory | ||
| public static Iterable<Object[]> parameters() { | ||
| return similarityParameters(L1Norm.class.getSimpleName(), L1Norm.SIMILARITY_FUNCTION); | ||
| } | ||
|
|
||
| protected EsqlCapabilities.Cap capability() { | ||
| return EsqlCapabilities.Cap.L1_NORM_VECTOR_SIMILARITY_FUNCTION; | ||
| } | ||
|
|
||
| @Override | ||
| protected Expression build(Source source, List<Expression> args) { | ||
| return new L1Norm(source, args.get(0), args.get(1)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!