@@ -65,7 +65,9 @@ ORDER BY VECTOR_DISTANCE('cosine', [v].[Vector], CAST('[1,2,100]' AS VECTOR(3)))
6565""" ) ;
6666 }
6767
68+ // The latest vector index version (required for VECTOR_SEARCH) is only available on Azure SQL (#36384).
6869 [ ConditionalFact ]
70+ [ SqlServerCondition ( SqlServerCondition . IsAzureSql ) ]
6971 [ Experimental ( "EF9105" ) ]
7072 public async Task VectorSearch_project_entity_and_distance ( )
7173 {
@@ -74,28 +76,32 @@ public async Task VectorSearch_project_entity_and_distance()
7476 var vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 100 } ) ;
7577
7678 var results = await ctx . VectorEntities
77- . VectorSearch ( e => e . Vector , similarTo : vector , "cosine" , topN : 1 )
79+ . VectorSearch ( e => e . Vector , similarTo : vector , "cosine" )
80+ . OrderBy ( e => e . Distance )
81+ . Take ( 1 )
7882 . ToListAsync ( ) ;
7983
8084 Assert . Equal ( 2 , results . Single ( ) . Value . Id ) ;
8185
8286 AssertSql (
8387 """
84- @p='Microsoft.Data.SqlTypes.SqlVector`1[System.Single]' (Size = 20) (DbType = Binary)
8588@p1='1'
89+ @p='Microsoft.Data.SqlTypes.SqlVector`1[System.Single]' (Size = 20) (DbType = Binary)
8690
87- SELECT [v].[Id], [v0].[Distance]
91+ SELECT TOP(@p1) WITH APPROXIMATE [v].[Id], [v0].[Distance]
8892FROM VECTOR_SEARCH(
8993 TABLE = [VectorEntities] AS [v],
9094 COLUMN = [Vector],
9195 SIMILAR_TO = @p,
92- METRIC = 'cosine',
93- TOP_N = @p1
96+ METRIC = 'cosine'
9497) AS [v0]
98+ ORDER BY [v0].[Distance]
9599""" ) ;
96100 }
97101
102+ // The latest vector index version (required for VECTOR_SEARCH) is only available on Azure SQL (#36384).
98103 [ ConditionalFact ]
104+ [ SqlServerCondition ( SqlServerCondition . IsAzureSql ) ]
99105 [ Experimental ( "EF9105" ) ]
100106 public async Task VectorSearch_project_entity_only_with_distance_filter_and_ordering ( )
101107 {
@@ -104,10 +110,11 @@ public async Task VectorSearch_project_entity_only_with_distance_filter_and_orde
104110 var vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 100 } ) ;
105111
106112 var results = await ctx . VectorEntities
107- . VectorSearch ( e => e . Vector , similarTo : vector , "cosine" , topN : 3 )
113+ . VectorSearch ( e => e . Vector , similarTo : vector , "cosine" )
108114 . Where ( e => e . Distance < 0.01 )
109115 . OrderBy ( e => e . Distance )
110116 . Select ( e => e . Value )
117+ . Take ( 3 )
111118 . ToListAsync ( ) ;
112119
113120 Assert . Collection (
@@ -117,16 +124,15 @@ public async Task VectorSearch_project_entity_only_with_distance_filter_and_orde
117124
118125 AssertSql (
119126 """
120- @p='Microsoft.Data.SqlTypes.SqlVector`1[System.Single]' (Size = 20) (DbType = Binary)
121127@p1='3'
128+ @p='Microsoft.Data.SqlTypes.SqlVector`1[System.Single]' (Size = 20) (DbType = Binary)
122129
123- SELECT [v].[Id]
130+ SELECT TOP(@p1) WITH APPROXIMATE [v].[Id]
124131FROM VECTOR_SEARCH(
125132 TABLE = [VectorEntities] AS [v],
126133 COLUMN = [Vector],
127134 SIMILAR_TO = @p,
128- METRIC = 'cosine',
129- TOP_N = @p1
135+ METRIC = 'cosine'
130136) AS [v0]
131137WHERE [v0].[Distance] < 0.01E0
132138ORDER BY [v0].[Distance]
@@ -167,23 +173,27 @@ public class VectorQueryContext(DbContextOptions options) : PoolableDbContext(op
167173
168174 public static async Task SeedAsync ( VectorQueryContext context )
169175 {
170- var vectorEntities = new VectorEntity [ ]
171- {
172- new ( ) { Id = 1 , Vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 3 } ) } ,
173- new ( ) { Id = 2 , Vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 100 } ) } ,
174- new ( ) { Id = 3 , Vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 1000 } ) }
175- } ;
176+ // SQL Server vector indexes require at least 100 rows.
177+ var vectorEntities = Enumerable . Range ( 1 , 100 ) . Select (
178+ i => new VectorEntity
179+ {
180+ Id = i ,
181+ Vector = new SqlVector < float > ( new float [ ] { i * 0.01f , i * 0.02f , i * 0.03f } )
182+ } ) . ToList ( ) ;
183+
184+ // Override specific rows we use in test assertions
185+ vectorEntities [ 0 ] = new VectorEntity { Id = 1 , Vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 3 } ) } ;
186+ vectorEntities [ 1 ] = new VectorEntity { Id = 2 , Vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 100 } ) } ;
187+ vectorEntities [ 2 ] = new VectorEntity { Id = 3 , Vector = new SqlVector < float > ( new float [ ] { 1 , 2 , 1000 } ) } ;
176188
177189 context . VectorEntities . AddRange ( vectorEntities ) ;
178190 await context . SaveChangesAsync ( ) ;
179191
180- // TODO (#36384): Remove this once it's out of preview
181192 await context . Database . ExecuteSqlAsync ( $ "ALTER DATABASE SCOPED CONFIGURATION SET PREVIEW_FEATURES = ON") ;
182193
183194 await context . Database . ExecuteSqlAsync ( $ """
184195CREATE VECTOR INDEX vec_idx ON VectorEntities(Vector)
185- WITH (METRIC = 'Cosine', TYPE = 'DiskANN')
186- ON [PRIMARY];
196+ WITH (METRIC = 'Cosine', TYPE = 'DiskANN');
187197""" ) ;
188198 }
189199 }
0 commit comments