ESQL - KNN functions with non-pushed down filters#131708
Closed
carlosdelest wants to merge 27 commits intoelastic:mainfrom
Closed
ESQL - KNN functions with non-pushed down filters#131708carlosdelest wants to merge 27 commits intoelastic:mainfrom
carlosdelest wants to merge 27 commits intoelastic:mainfrom
Conversation
…e that will be detected later.
carlosdelest
commented
Jul 22, 2025
| assert block instanceof DocBlock : "LuceneQueryExpressionEvaluator expects DocBlock as input"; | ||
| DocVector docs = (DocVector) block.asVector(); | ||
| // Search for DocVector block | ||
| Block docBlock = null; |
Member
Author
There was a problem hiding this comment.
Made LuceneQueryEvaluator more robust
| assert page.getBlockCount() >= 2 : "Expected at least 2 blocks, got " + page.getBlockCount(); | ||
| assert page.getBlock(0).asVector() instanceof DocVector : "Expected a DocVector, got " + page.getBlock(0).asVector(); | ||
| assert page.getBlock(1).asVector() instanceof DoubleVector : "Expected a DoubleVector, got " + page.getBlock(1).asVector(); | ||
| assert page.getBlockCount() > scoreBlockPosition : "Expected to get a score block in position " + scoreBlockPosition; |
Member
Author
There was a problem hiding this comment.
This was an uncovered bug
| from colors metadata _score | ||
| | eval composed_name = locate(color, " ") > 0 | ||
| | where knn(rgb_vector, [128,128,0], 140) and composed_name == false | ||
| | where knn(rgb_vector, [128,128,0], 10) and composed_name == false |
Member
Author
There was a problem hiding this comment.
We can see the change in action - we no longer need to use a large number for k to maintain semantics, nor to use limit at the end.
| if (failures.hasFailures() == false) { | ||
| if (p instanceof PostOptimizationVerificationAware pova) { | ||
| pova.postOptimizationVerification(failures); | ||
| } else if (p instanceof PostOptimizationPlanVerificationAware popva) { |
Member
Author
There was a problem hiding this comment.
Added PostOptimizationPlanVerificationAware to have the plan available for validations. This is similar to PostAnalysisPlanVerificationAware
@bpintea , WDYT?
…er-non-pushed-down # Conflicts: # x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/EsqlFunctionRegistry.java # x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java
8 tasks
Collaborator
|
Pinging @elastic/es-analytical-engine (Team:Analytics) |
Collaborator
|
Pinging @elastic/es-search-relevance (Team:Search Relevance) |
…n-pushed-down' into non-issue/knn-prefilter-non-pushed-down
Member
Author
|
Closing as non-pushed down prefilters will make knn use exact search instead, see #132944 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
KNN functions include other filter conjunctions as pre-filters as per #131004.
However, when a KNN function has non-pushable expressions as prefilters, it cannot be pushed down to Lucene. KNN can be executed in the compute engine, but the prefilters will be executed as postfilters. This means than less than the top k results can be retrieved from the KNN function.
In order to retrieve the true top k results and maintain knn semantics, the following transformation will be made:
A query like:
WHERE knn(field1, [..], 10) AND non-pushable-filterWill be replaced with:
This way, knn becomes an exact search issued after the non-pushable filters.
The score for knn is then calculated to obtain the top K, and scores of zero are removed (could happen if minimum similarity is used).
This aims to maintain knn semantics and avoid filters to act as post-filters.