-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Add support for knn vector queries on semantic_text fields #119011
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
Add support for knn vector queries on semantic_text fields #119011
Conversation
Hi @kderusso, I've created a changelog YAML for you. |
@@ -5,7 +5,7 @@ | |||
* 2.0. | |||
*/ | |||
|
|||
package org.elasticsearch.xpack.ml.vectors; | |||
package org.elasticsearch.xpack.core.ml.vectors; |
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.
This had to move from the ml
plugin in order to access it in the inference
plugin. While I could have moved it to inference
, core
seemed like a better fit for this standalone class. I did keep the tests in the ml
plugin however, because they rely on the ML test runner.
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.
Partial review. Great start to this! As far as edge cases, I think we need some test cases for the following:
- Missing both query vector and query vector builder
- Querying multiple indices that use different inference IDs, particularly when each index has more than
k
potential matches
...e/src/main/java/org/elasticsearch/xpack/core/ml/vectors/TextEmbeddingQueryVectorBuilder.java
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Show resolved
Hide resolved
Pinging @elastic/es-search-relevance (Team:Search Relevance) |
Pinging @elastic/search-eng (Team:SearchOrg) |
Pinging @elastic/search-relevance (Team:Search - Relevance) |
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.
LGTM 💯
Some questions about pulling up some common methods - can be done in follow up work
...e/src/main/java/org/elasticsearch/xpack/core/ml/vectors/TextEmbeddingQueryVectorBuilder.java
Outdated
Show resolved
Hide resolved
...e/src/main/java/org/elasticsearch/xpack/core/ml/vectors/TextEmbeddingQueryVectorBuilder.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Show resolved
Hide resolved
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class SemanticKnnVectorQueryRewriteInterceptorTests extends ESTestCase { |
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.
We may find a common structure for these tests and create a base class for them in follow up work
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.
Yes, I would like to have this organized better in a followup. Unfortunately, since the interceptor is a QueryBuilder
we can't take advantage of built in abstract query builder tests. I was trying to focus more on test functionality first, so we don't back ourselves into a corner. Right now I'm thinking this would be a better followup optimization if the test cases are complete though. WDYT?
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.
Yes, I'm totally fine with doing that as follow up work 👍
...n/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/47_semantic_text_knn.yml
Outdated
Show resolved
Hide resolved
...n/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/47_semantic_text_knn.yml
Show resolved
Hide resolved
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.
Partial review of everything except for the tests. Good work on this, I have some suggestions about how we can make the intercept logic simpler and more efficient.
...e/src/main/java/org/elasticsearch/xpack/core/ml/vectors/TextEmbeddingQueryVectorBuilder.java
Outdated
Show resolved
Hide resolved
createSubQueryForIndices( | ||
inferenceIdsIndices.get(inferenceId), | ||
buildNestedQueryFromKnnVectorQuery(queryBuilder, inferenceId) | ||
) |
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.
I think we could update buildNestedQueryFromKnnVectorQuery
to take the index list and build the nested knn
query that filters on the indices directly in it. This would both simplify the code and be more efficient, as it looks like right now you're effectively using the query builder returned from buildNestedQueryFromKnnVectorQuery
as a wrapper for data that you use to build the actual nested query in createSubQueryForIndices
.
Another benefit of this approach is that we remove the index post-filtering (via the boolean query), which is no longer necessary since we are pre-filtering on index in the knn
query.
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.
Good call on adding these as a prefilter, however the post filtering can't be removed without breakinng functionality for dense vector combined fields. I think your suggestions helped to clean it up a bit though.
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...c/test/java/org/elasticsearch/index/query/SemanticKnnVectorQueryRewriteInterceptorTests.java
Outdated
Show resolved
Hide resolved
...c/test/java/org/elasticsearch/index/query/SemanticKnnVectorQueryRewriteInterceptorTests.java
Outdated
Show resolved
Hide resolved
...n/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/47_semantic_text_knn.yml
Outdated
Show resolved
Hide resolved
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.
Thanks for making the changes! There's one more follow-up, I think we can avoid index post-filtering when not using nested queries. Please correct me if I'm wrong though.
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
...java/org/elasticsearch/xpack/inference/queries/SemanticKnnVectorQueryRewriteInterceptor.java
Outdated
Show resolved
Hide resolved
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.
LGTM, thanks for the iterations!
…19011) * First cut at KNN interceptor * Move TextEmbeddingQueryVectorBuilder to xpack-core * Infer model ID * Fix test compilation errors * Update docs/changelog/119011.yaml * Update changelog * Update test * Cleanup * PR feedback * Add yaml test * Cleanup tests * Update test * Minor PR feedback * refactor pre filter indices for knn * PR feedback
…119648) * First cut at KNN interceptor * Move TextEmbeddingQueryVectorBuilder to xpack-core * Infer model ID * Fix test compilation errors * Update docs/changelog/119011.yaml * Update changelog * Update test * Cleanup * PR feedback * Add yaml test * Cleanup tests * Update test * Minor PR feedback * refactor pre filter indices for knn * PR feedback
Supports
knn
queries in the query DSL againstsemantic_text
fields. Note that top levelknn
search using theknn
section is not supported.Example script: