Skip to content

Conversation

@mridula-s109
Copy link
Contributor

@mridula-s109 mridula-s109 commented Sep 14, 2025

Summary

Small cleanup to bring SparseVectorQueryBuilder in line with current inference APIs and the pattern used by SemanticQueryBuilder.

Changes

  • Replace CoordinatedInferenceAction.Request with InferenceAction.Request (parity with SemanticQueryBuilder).
  • Remove outdated TODO about moving this to xpack core (it’s already in core).
  • Refactor response handling to a validateAndExtractTextExpansionResults(...) helper for clearer errors and consistency.

@mridula-s109
Copy link
Contributor Author

Hi @kderusso 👋

This draft PR addresses the SparseVectorQueryBuilder refactoring.

Highlights:

  • Standardized on InferenceAction.Request (no need for CoordinatedInferenceAction.Request)
  • Clean separation from ML coordination logic
  • Minimal, mostly mechanical refactoring — tests all pass

Would love your thoughts, especially on whether the InferenceAction.Request standardization and overall approach match the intended architectural direction. 🙏

Copy link
Member

@kderusso kderusso left a comment

Choose a reason for hiding this comment

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

Hey @mridula-s109 so sorry about the confusion here, the Jira ticket you were assigned said that Sparse Vector was still in ml and we didn't verify that it had already been moved to core during planning 😅

I think the cleanup refactoring that you've done here makes sense for the most part, with some caveats:

  • I would like to understand the difference between a CoordinatedInferenceAction and an InferenceAction. By changing this, what is actually changing under the hood and what side effects are there? Knowing that someone has done this research is important for peace of mind
  • The bulk of the work for this type of refactor lies in test failures - and there are a lot of these in your PR.

It would be worth thinking about whether we need this PR, or since it's already in core if we just try to do a refactoring to server.

@mridula-s109
Copy link
Contributor Author

Hey @mridula-s109 so sorry about the confusion here, the Jira ticket you were assigned said that Sparse Vector was still in ml and we didn't verify that it had already been moved to core during planning 😅

I think the cleanup refactoring that you've done here makes sense for the most part, with some caveats:

  • I would like to understand the difference between a CoordinatedInferenceAction and an InferenceAction. By changing this, what is actually changing under the hood and what side effects are there? Knowing that someone has done this research is important for peace of mind
  • The bulk of the work for this type of refactor lies in test failures - and there are a lot of these in your PR.

It would be worth thinking about whether we need this PR, or since it's already in core if we just try to do a refactoring to server.

Hey @kderusso, thanks for clarifying!Since sparse vector is already in core, would you be okay with me closing this PR and also resolving the Jira issue?

@kderusso
Copy link
Member

Hey @kderusso, thanks for clarifying!Since sparse vector is already in core, would you be okay with me closing this PR and also resolving the Jira issue?

Can you please open a smaller PR to clear out the TODOs to avoid confusion down the line?

@kderusso
Copy link
Member

Also, did you time box looking into moving this to server? How bad would it be?

@kderusso
Copy link
Member

kderusso commented Oct 1, 2025

I noticed that sparse vector yaml tests are still in ML, if we're keeping this PR we should probably move them (unless there's a compelling "this will break everything" reason to keep them)

@mridula-s109 mridula-s109 force-pushed the refactor_sparsevectorquerybuilder branch from 3ff23bf to 2b3a991 Compare October 2, 2025 15:30
@mridula-s109
Copy link
Contributor Author

Hey @mridula-s109 so sorry about the confusion here, the Jira ticket you were assigned said that Sparse Vector was still in ml and we didn't verify that it had already been moved to core during planning 😅

I think the cleanup refactoring that you've done here makes sense for the most part, with some caveats:

  • I would like to understand the difference between a CoordinatedInferenceAction and an InferenceAction. By changing this, what is actually changing under the hood and what side effects are there? Knowing that someone has done this research is important for peace of mind
  • The bulk of the work for this type of refactor lies in test failures - and there are a lot of these in your PR.

It would be worth thinking about whether we need this PR, or since it's already in core if we just try to do a refactoring to server.

Thanks for the thoughtful feedback @kderusso and @ioanatia 🙏

To clarify the CoordinatedInferenceAction vs InferenceAction part:

  • CoordinatedInferenceAction is used when we need to coordinate inference requests across clusters (CCS mode, reduce roundtrips=false, etc.). It wraps results into a “coordination format.”
  • InferenceAction is the simpler path for local inference requests within a single cluster. It doesn’t do the extra cross-cluster handling.

Since SparseVector queries don’t currently need cross-cluster coordination, switching to InferenceAction.Request aligns with what we already do in SemanticQueryBuilder. Functionally, this shouldn’t change behaviour for local queries, but it does reduce unnecessary complexity.

On the scope of the refactor:

  • You’re right that SparseVectorQueryBuilder is already in core, so the big move work is unnecessary.
  • I’ve now aligned it with the SemanticQueryBuilder pattern (as suggested by Ioana) and cleaned up the TODO to avoid future confusion.
  • The remaining open question is whether we should take the next step of moving this fully into server (like knn). I haven’t deeply time-boxed that yet my sense is it’s a larger lift and may be best tracked separately.

@mridula-s109 mridula-s109 changed the title Refactor SparseVectorQueryBuilder out of ML plugin Cleanup: Align SparseVectorQueryBuilder with InferenceAction and remove legacy TODO Oct 2, 2025
@mridula-s109 mridula-s109 changed the title Cleanup: Align SparseVectorQueryBuilder with InferenceAction and remove legacy TODO Cleanup: Align SparseVectorQueryBuilder with InferenceAction Oct 2, 2025
@mridula-s109 mridula-s109 marked this pull request as ready for review October 2, 2025 15:59
@elasticsearchmachine elasticsearchmachine added the needs:triage Requires assignment of a team area label label Oct 2, 2025
@mridula-s109 mridula-s109 added Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch Team:Search - Relevance The Search organization Search Relevance team labels Oct 2, 2025
@elasticsearchmachine elasticsearchmachine removed Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch Team:Search - Relevance The Search organization Search Relevance team labels Oct 2, 2025
@mridula-s109
Copy link
Contributor Author

mridula-s109 commented Oct 2, 2025

Hey @mridula-s109 so sorry about the confusion here, the Jira ticket you were assigned said that Sparse Vector was still in ml and we didn't verify that it had already been moved to core during planning 😅
I think the cleanup refactoring that you've done here makes sense for the most part, with some caveats:

  • I would like to understand the difference between a CoordinatedInferenceAction and an InferenceAction. By changing this, what is actually changing under the hood and what side effects are there? Knowing that someone has done this research is important for peace of mind
  • The bulk of the work for this type of refactor lies in test failures - and there are a lot of these in your PR.

It would be worth thinking about whether we need this PR, or since it's already in core if we just try to do a refactoring to server.

Thanks for the thoughtful feedback @kderusso and @ioanatia 🙏

To clarify the CoordinatedInferenceAction vs InferenceAction part:

  • CoordinatedInferenceAction is used when we need to coordinate inference requests across clusters (CCS mode, reduce roundtrips=false, etc.). It wraps results into a “coordination format.”
  • InferenceAction is the simpler path for local inference requests within a single cluster. It doesn’t do the extra cross-cluster handling.

Since SparseVector queries don’t currently need cross-cluster coordination, switching to InferenceAction.Request aligns with what we already do in SemanticQueryBuilder. Functionally, this shouldn’t change behaviour for local queries, but it does reduce unnecessary complexity.

On the scope of the refactor:

  • You’re right that SparseVectorQueryBuilder is already in core, so the big move work is unnecessary.
  • I’ve now aligned it with the SemanticQueryBuilder pattern (as suggested by Ioana) and cleaned up the TODO to avoid future confusion.
  • The remaining open question is whether we should take the next step of moving this fully into server (like knn). I haven’t deeply time-boxed that yet my sense is it’s a larger lift and may be best tracked separately.

I tested everything locally with ./gradlew :x-pack:plugin:inference:check and all the tests passed. I’ll also double-check any failures once CI finishes running.

@kderusso
Copy link
Member

kderusso commented Oct 2, 2025

Since SparseVector queries don’t currently need cross-cluster coordination,

Is that true, or would that break CCS for sparse vector search?

@ioanatia
Copy link
Contributor

ioanatia commented Oct 6, 2025

Since SparseVector queries don’t currently need cross-cluster coordination,

Is that true, or would that break CCS for sparse vector search?

This does not break CCS support. The CCS support that we recently added for SparseVectorQueryBuilder kicks in only when we query semantic_text fields with sparse_vector. In that case, the query interceptor will be responsible for getting the inference results and rewriting the sparse_vector query. So the code path that Mridula touches here will not even be reached.

Taking a closer look at CoordinatedInferenceAction its purpose is to be a unified, internal API that can handle multiple types of tasks and that is able to call the inference API or internal ML models (using the infer model internal API).
If used with inference endpoints that refer to Elasticsearch models hosted on the ML node, it will call the infer model API directly.
The same InferModelAction is called by the inference service, when used with an inference endpoint that points to an ML model that is being run by ML Elasticsearch nodes.
I don't really see how any of these would break CCS.

This change actually breaks something else, in a more subtle way.
Right now on main, for the sparse_vector query, the inference_id can point to both an Inference Endpoint and a model ID.

On this branch, if inference_id points to a model ID, the query will break.
However, I don't think we ever intended to support model_id, the inference_id option was always documented as an inference API endpoint.

@elasticsearchmachine elasticsearchmachine removed the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Oct 8, 2025
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine
Copy link
Collaborator

Hi @mridula-s109, I've created a changelog YAML for you.

@mridula-s109
Copy link
Contributor Author

mridula-s109 commented Oct 29, 2025

As its already removed to core, just removed the redundant todo @kderusso . We can evaluate moving to server later when necessary.

@mridula-s109 mridula-s109 changed the title Cleanup: Align SparseVectorQueryBuilder with InferenceAction Cleanup:SparseVectorQueryBuilder with InferenceAction Oct 29, 2025
@mridula-s109 mridula-s109 changed the title Cleanup:SparseVectorQueryBuilder with InferenceAction Cleanup: Remove obsolete TODO from SparseVectorQueryBuilder Oct 29, 2025
…ml/search/SparseVectorQueryBuilder.java

Co-authored-by: Kathleen DeRusso <[email protected]>
@mridula-s109 mridula-s109 requested a review from kderusso October 29, 2025 15:01
Copy link
Member

@kderusso kderusso left a comment

Choose a reason for hiding this comment

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

:shipit:

@mridula-s109 mridula-s109 merged commit 42049d1 into elastic:main Oct 30, 2025
34 checks passed
chrisparrinello pushed a commit to chrisparrinello/elasticsearch that referenced this pull request Nov 3, 2025
…134702)

* Removed the TODO

* Updated the sparevector similar to Semantic query builder

* Update docs/changelog/134702.yaml

* Remove obsolete TODO comment from SparseVectorQueryBuilder

* [CI] Auto commit changes from spotless

* Modified the TODO to make more sense

* Delete docs/changelog/134702.yaml

* Update x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/search/SparseVectorQueryBuilder.java

Co-authored-by: Kathleen DeRusso <[email protected]>

* Update SparseVectorQueryBuilder.java

---------

Co-authored-by: elasticsearchmachine <[email protected]>
Co-authored-by: Kathleen DeRusso <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>non-issue :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.

4 participants