Skip to content

Commit 8e6b1f4

Browse files
Copilotroji
andauthored
docs: update vector search docs to reflect VectorSearch() API changes (removal of topN parameter)
The topN parameter was removed from VectorSearch() in dotnet/efcore#38075. Users now compose with .OrderBy(r => r.Distance).Take(n) instead. SQL Server generates TOP(...) WITH APPROXIMATE rather than TOP_N inside VECTOR_SEARCH(). Agent-Logs-Url: https://github.com/dotnet/EntityFramework.Docs/sessions/a44e53cf-0113-4f96-b07b-4e403b6e0e9b Co-authored-by: roji <1862641+roji@users.noreply.github.com>
1 parent 845a66c commit 8e6b1f4

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

entity-framework/core/providers/sql-server/vector-search.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ Once you have a vector index, use the `VectorSearch()` extension method on your
129129

130130
```csharp
131131
var blogs = await context.Blogs
132-
.VectorSearch(b => b.Embedding, embedding, "cosine", topN: 5)
132+
.VectorSearch(b => b.Embedding, embedding, "cosine")
133+
.OrderBy(r => r.Distance)
134+
.Take(5)
133135
.ToListAsync();
134136

135137
foreach (var (blog, score) in blogs)
@@ -141,18 +143,26 @@ foreach (var (blog, score) in blogs)
141143
This translates to the following SQL:
142144

143145
```sql
144-
SELECT [v].[Id], [v].[Name], [v].[Distance]
145-
FROM VECTOR_SEARCH([Blogs], 'Embedding', @__embedding, 'metric = cosine', @__topN)
146+
SELECT TOP(@p) WITH APPROXIMATE [b].[Id], [b].[Name], [v].[Distance]
147+
FROM VECTOR_SEARCH(
148+
TABLE = [Blogs] AS [b],
149+
COLUMN = [Embedding],
150+
SIMILAR_TO = @p1,
151+
METRIC = 'cosine'
152+
) AS [v]
153+
ORDER BY [v].[Distance]
146154
```
147155

148-
The `topN` parameter specifies the maximum number of results to return.
156+
Compose `VectorSearch()` with `OrderBy(r => r.Distance)` and `Take(...)` to limit the number of results returned as required for approximate vector search.
149157

150158
`VectorSearch()` returns `VectorSearchResult<TEntity>`, which allows you to access both the entity and the computed distance:
151159

152160
```csharp
153161
var searchResults = await context.Blogs
154-
.VectorSearch(b => b.Embedding, embedding, "cosine", topN: 5)
162+
.VectorSearch(b => b.Embedding, embedding, "cosine")
155163
.Where(r => r.Distance < 0.05)
164+
.OrderBy(r => r.Distance)
165+
.Take(5)
156166
.Select(r => new { Blog = r.Value, Distance = r.Distance })
157167
.ToListAsync();
158168
```
@@ -175,7 +185,10 @@ var results = await context.Articles
175185
.FreeTextTable<Article, int>(textualQuery, topN: k)
176186
// Perform vector (semantic) search, joining the results of both searches together
177187
.LeftJoin(
178-
context.Articles.VectorSearch(b => b.Embedding, queryEmbedding, "cosine", topN: k),
188+
context.Articles
189+
.VectorSearch(b => b.Embedding, queryEmbedding, "cosine")
190+
.OrderBy(r => r.Distance)
191+
.Take(k),
179192
fts => fts.Key,
180193
vs => vs.Value.Id,
181194
(fts, vs) => new
@@ -211,12 +224,15 @@ The query produces the following SQL:
211224
```sql
212225
SELECT TOP(@p3) [a0].[Id], [a0].[Content], [a0].[Title]
213226
FROM FREETEXTTABLE([Articles], *, @p, @p1) AS [f]
214-
LEFT JOIN VECTOR_SEARCH(
215-
TABLE = [Articles] AS [a0],
216-
COLUMN = [Embedding],
217-
SIMILAR_TO = @p2,
218-
METRIC = 'cosine',
219-
TOP_N = @p3
220-
) AS [v] ON [f].[KEY] = [a0].[Id]
221-
ORDER BY 1.0E0 / CAST(10 + [f].[RANK] AS float) + ISNULL(1.0E0 / (10.0E0 + [v].[Distance]), 0.0E0) DESC
227+
LEFT JOIN (
228+
SELECT TOP(@p4) WITH APPROXIMATE [a0].[Id], [a0].[Content], [a0].[Title], [v].[Distance]
229+
FROM VECTOR_SEARCH(
230+
TABLE = [Articles] AS [a0],
231+
COLUMN = [Embedding],
232+
SIMILAR_TO = @p2,
233+
METRIC = 'cosine'
234+
) AS [v]
235+
ORDER BY [v].[Distance]
236+
) AS [v0] ON [f].[KEY] = [v0].[Id]
237+
ORDER BY 1.0E0 / CAST(10 + [f].[RANK] AS float) + ISNULL(1.0E0 / (10.0E0 + [v0].[Distance]), 0.0E0) DESC
222238
```

entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,13 @@ Once you have a vector index, you can use the `VectorSearch()` extension method
230230

231231
```csharp
232232
var blogs = await context.Blogs
233-
.VectorSearch(b => b.Embedding, embedding, "cosine", topN: 5)
233+
.VectorSearch(b => b.Embedding, embedding, "cosine")
234+
.OrderBy(r => r.Distance)
235+
.Take(5)
234236
.ToListAsync();
235237
```
236238

237-
This translates to the SQL Server [`VECTOR_SEARCH()`](/sql/t-sql/functions/vector-search-transact-sql) table-valued function, which performs an approximate search over the vector index. The `topN` parameter specifies the number of results to return.
239+
This translates to the SQL Server [`VECTOR_SEARCH()`](/sql/t-sql/functions/vector-search-transact-sql) table-valued function, which performs an approximate search over the vector index. Compose with `OrderBy(r => r.Distance)` and `Take(...)` to limit the number of results returned.
238240

239241
`VectorSearch()` returns `VectorSearchResult<TEntity>`, allowing you to access the distance alongside the entity.
240242

0 commit comments

Comments
 (0)