Språk

SqlServerQueryableExtensions.VectorSearch<T,TVector> Method

Definition

Search for vectors similar to a given query vector using the SQL Server VECTOR_SEARCH() function.

[System.Diagnostics.CodeAnalysis.Experimental("EF9105")]
public static System.Linq.IQueryable<Microsoft.EntityFrameworkCore.VectorSearchResult<T>> VectorSearch<T,TVector>(this Microsoft.EntityFrameworkCore.DbSet<T> source, System.Linq.Expressions.Expression<Func<T,TVector>> vectorPropertySelector, TVector similarTo, string metric) where T : class where TVector : struct;
[<System.Diagnostics.CodeAnalysis.Experimental("EF9105")>]
static member VectorSearch : Microsoft.EntityFrameworkCore.DbSet<'T (requires 'T : null)> * System.Linq.Expressions.Expression<Func<'T, 'Vector>> * 'Vector * string -> System.Linq.IQueryable<Microsoft.EntityFrameworkCore.VectorSearchResult<'T>> (requires 'T : null and 'Vector : struct)
<Extension()>
Public Function VectorSearch(Of T As Class, TVector As Class) (source As DbSet(Of T), vectorPropertySelector As Expression(Of Func(Of T, TVector)), similarTo As TVector, metric As String) As IQueryable(Of VectorSearchResult(Of T))

Type Parameters

T
TVector

Parameters

source
DbSet<T>

The DbSet<TEntity> representing the table containing the vector column to query.

vectorPropertySelector
Expression<Func<T,TVector>>

A selector for the vector property on the entity.

similarTo
TVector

The vector used for search, either a parameter or another vector column.

metric
String

The distance metric used to calculate the distance between the query vector and the vectors in the specified column. An ANN (Approximate Nearest Neighbor) index is used only if a matching ANN index, with the same metric and on the same column, is found. If there are no compatible ANN indexes, a warning is raised and the KNN (k-Nearest Neighbor) algorithm is used.

Returns

Attributes

Remarks

Use WithApproximate<T>(IQueryable<T>) after Take(...) to enable approximate nearest neighbor (ANN) search, which uses the vector index for better performance. Without WithApproximate, an exact k-nearest neighbor (kNN) search is performed. Add an explicit OrderBy to control result ordering.

Applies to