Skip to content

Add implicit ORDER BY Distance for VectorSearch translation#38086

Open
roji wants to merge 3 commits intomainfrom
roji/vector-search-updates
Open

Add implicit ORDER BY Distance for VectorSearch translation#38086
roji wants to merge 3 commits intomainfrom
roji/vector-search-updates

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Apr 11, 2026

Tiny follow-up to #38075.

VECTOR_SEARCH() results are inherently ordered by distance ascending. This adds the ordering implicitly during translation so users can compose with Take() without needing an explicit OrderBy(r => r.Distance).

Before:

ctx.VectorEntities
    .VectorSearch(e => e.Vector, similarTo: vector, "cosine")
    .OrderBy(e => e.Distance)  // required boilerplate
    .Take(3)
    .ToListAsync();

After:

ctx.VectorEntities
    .VectorSearch(e => e.Vector, similarTo: vector, "cosine")
    .Take(3)  // implicit ORDER BY Distance ASC
    .ToListAsync();

An explicit .OrderBy() or .OrderByDescending() still overrides the implicit ordering.

We could adopt a more low-level/conservative approach, saying that since SQL Server requires the explicit ORDER BY at the SQL level, we should continue requiring it at the LINQ level. However, we already add implicit orderings elsewhere (especially when transforming JSON arrays to resultsets with OPENJSON), and I don't see the point of having a mandatory, explicit gesture where we can save users the trouble.

VECTOR_SEARCH() results are inherently ordered by distance ascending.
Add this ordering implicitly during translation so users can compose
with Take() without needing an explicit OrderBy(r => r.Distance).

Also remove the unnecessary forDml guard from GenerateTop() since
VectorSearch() requires DbSet<T> and cannot appear in DELETE/UPDATE
table lists through LINQ.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@roji roji requested a review from a team as a code owner April 11, 2026 07:38
Copilot AI review requested due to automatic review settings April 11, 2026 07:38
@roji roji linked an issue Apr 11, 2026 that may be closed by this pull request
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates EFCore.SqlServer’s VectorSearch() translation to automatically apply ORDER BY Distance ASC, reflecting SQL Server’s VECTOR_SEARCH() natural ordering so that users can compose with Take() without adding boilerplate OrderBy(r => r.Distance).

Changes:

  • Add an implicit ORDER BY Distance ASC to the translated VECTOR_SEARCH() query.
  • Update SQL Server functional tests to remove redundant explicit OrderBy(Distance) calls.
  • Update VectorSearch() XML docs to describe the new implicit ordering behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
test/EFCore.SqlServer.FunctionalTests/Query/Translations/VectorTranslationsSqlServerTest.cs Removes explicit OrderBy(Distance) from existing VectorSearch tests while keeping ORDER BY in generated SQL baselines.
src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs Introduces a shared Distance column projection and appends an implicit ordering by that column.
src/EFCore.SqlServer/Extensions/SqlServerQueryableExtensions.cs Updates API remarks to state results are implicitly ordered by distance ascending.

@roji
Copy link
Copy Markdown
Member Author

roji commented Apr 11, 2026

@copilot address the above unresolved comments.

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 11, 2026

@copilot address the above unresolved comments.

Done in e8af99c:

  1. Renamed VectorSearch_project_entity_only_with_distance_filter_and_orderingVectorSearch_project_entity_only_with_distance_filter since the ordering is now implicit.
  2. Extended IsNaturallyOrdered in SqlServerQueryableMethodTranslatingExpressionVisitor to also recognize VECTOR_SEARCH ordering by Distance ASC as a natural ordering, consistent with how OpenJson ordering is handled. This prevents spurious Distinct-after-OrderBy warnings when composing with .Distinct().

Copilot finished work on behalf of roji April 11, 2026 07:51
Copilot AI review requested due to automatic review settings April 11, 2026 16:43
@roji roji review requested due to automatic review settings April 11, 2026 16:43
Copilot finished work on behalf of roji April 11, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL Server: Implement support for VECTOR_SEARCH()

4 participants