Skip to content

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
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

knn query on semantic_text field with semantic_text filter fails

2 participants