Skip to content

Intercept filters to knn queries#138457

Merged
dimitris-athanasiou merged 7 commits intoelastic:mainfrom
dimitris-athanasiou:intercept-knn-filters
Nov 27, 2025
Merged

Intercept filters to knn queries#138457
dimitris-athanasiou merged 7 commits intoelastic:mainfrom
dimitris-athanasiou:intercept-knn-filters

Conversation

@dimitris-athanasiou
Copy link
Contributor

knn queries can have filter queries. Those filters may contain semantic queries (e.g. knn, sparse_vector, or match queries targetting semantic_text fields). We also need to intercept those in order to perform inference for their query field during the coordinator node rewrite.

This commit achieves this with the following changes:

  • SemanticKnnVectorQueryRewriteInterceptor attempts to rewrite filter queries. This means we rely on a rewrite cycle to attempt to intercept every query down the tree of each filter query.

  • InterceptedInferenceQueryBuilder now has a customCoordinatorNodeRewrite method that subclasses can implement to implement additional rewriting logic needed, e.g. rewrite inner queries.

  • InterceptedInferenceKnnVectorQueryBuilder implements customCoordinatorNodeRewrite so that the filter queries are rewritten.

This commit fixes the exceptions throws in #138410. However, searches that contain semantic text queries as filters to a semantic text knn query will return 0 hits due to another issue that is captured in #138184.

Closes #138410

`knn` queries can have filter queries. Those filters may contain
semantic queries (e.g. `knn`, `sparse_vector`, or `match` queries
targetting `semantic_text` fields). We also need to intercept those
in order to perform inference for their `query` field during the
coordinator node rewrite.

This commit achieves this with the following changes:

- `SemanticKnnVectorQueryRewriteInterceptor` attempts to rewrite filter queries.
This means we rely on a rewrite cycle to attempt to intercept every query down the
tree of each filter query.

- `InterceptedInferenceQueryBuilder` now has a `customCoordinatorNodeRewrite` method
that subclasses can implement to implement additional rewriting logic needed, e.g. rewrite
inner queries.

- `InterceptedInferenceKnnVectorQueryBuilder` implements `customCoordinatorNodeRewrite` so
that the filter queries are rewritten.

This commit fixes the exceptions throws in elastic#138410. However, searches that contain semantic text
queries as filters to a semantic text knn query will return `0` hits due to another issue that
is captured in elastic#138184.

Closes elastic#138410
@dimitris-athanasiou dimitris-athanasiou added >bug :SearchOrg/Relevance Label for the Search (solution/org) Relevance team labels Nov 23, 2025
@dimitris-athanasiou dimitris-athanasiou requested a review from a team as a code owner November 23, 2025 11:16
@elasticsearchmachine elasticsearchmachine added the Team:Search - Relevance The Search organization Search Relevance team label Nov 23, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/search-relevance (Team:Search - Relevance)

@elasticsearchmachine
Copy link
Collaborator

Hi @dimitris-athanasiou, I've created a changelog YAML for you.

@dimitris-athanasiou dimitris-athanasiou requested review from Mikep86 and removed request for a team November 23, 2025 11:18
Copy link
Contributor

@Mikep86 Mikep86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 🙌 ! Looks like all the pieces are there, just a bit of adjustment & cleanup left.

@dimitris-athanasiou
Copy link
Contributor Author

@Mikep86 Thank you for very useful feedback. I believe I have addressed it all in f810b69

Copy link
Contributor

@Mikep86 Mikep86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very close to ready 🚀 . I found a couple more adjustments to make before we finalize this.

Copy link
Contributor

@Mikep86 Mikep86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending green CI 🚀

@dimitris-athanasiou dimitris-athanasiou merged commit 637e498 into elastic:main Nov 27, 2025
34 checks passed
@dimitris-athanasiou dimitris-athanasiou deleted the intercept-knn-filters branch November 27, 2025 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>bug :SearchOrg/Relevance Label for the Search (solution/org) Relevance team Team:Search - Relevance The Search organization Search Relevance team v9.3.0

3 participants