You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `vector` data type is available in SQL Server for storing high-dimensional vectors, commonly used for:
173
+
174
+
- Semantic search with embeddings
175
+
- Recommendation systems
176
+
- Similarity matching
177
+
- Machine learning applications
178
+
179
+
NOTE: general `halfvec` type support is unavailable because this feature is still in preview. See the Microsoft docs: [Vector data type](https://learn.microsoft.com/en-us/sql/t-sql/data-types/vector-data-type).
180
+
181
+
#### Usage
182
+
183
+
```typescript
184
+
@Entity()
185
+
exportclassDocumentChunk {
186
+
@PrimaryGeneratedColumn()
187
+
id:number
188
+
189
+
@Column("varchar")
190
+
content:string
191
+
192
+
// Vector column with 1998 dimensions
193
+
@Column("vector", { length: 1998 })
194
+
embedding:number[]
195
+
}
196
+
```
197
+
198
+
#### Vector Similarity Search
199
+
200
+
SQL Server provides the `VECTOR_DISTANCE` function for calculating distances between vectors:
201
+
202
+
```typescript
203
+
const queryEmbedding = [
204
+
/* your query vector */
205
+
]
206
+
207
+
const results =awaitdataSource.query(
208
+
`
209
+
DECLARE @question AS VECTOR (1998) = @0;
210
+
SELECT TOP (10) dc.*,
211
+
VECTOR_DISTANCE('cosine', @question, embedding) AS distance
212
+
FROM document_chunk dc
213
+
ORDER BY VECTOR_DISTANCE('cosine', @question, embedding)
214
+
`,
215
+
[JSON.stringify(queryEmbedding)],
216
+
)
217
+
```
218
+
219
+
**Distance Metrics:**
220
+
221
+
-`'cosine'` - Cosine distance (most common for semantic search)
222
+
-`'euclidean'` - Euclidean (L2) distance
223
+
-`'dot'` - Negative dot product
224
+
225
+
**Requirements:**
226
+
227
+
- SQL Server version with vector support enabled
228
+
- Vector dimensions must be specified using the `length` option
Copy file name to clipboardExpand all lines: docs/docs/entity/1-entities.md
+38-17Lines changed: 38 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -182,16 +182,17 @@ There are several special column types with additional functionality available:
182
182
183
183
### Vector columns
184
184
185
-
Vector columns are supported on both PostgreSQL (via [`pgvector`](https://github.com/pgvector/pgvector) extension) and SAP HANA Cloud, enabling storing and querying vector embeddings for similarity search and machine learning applications.
185
+
Vector columns are supported on PostgreSQL (via [`pgvector`](https://github.com/pgvector/pgvector) extension), Microsoft SQL Server, and SAP HANA Cloud, enabling storing and querying vector embeddings for similarity search and machine learning applications.
186
186
187
187
TypeORM supports both `vector` and `halfvec` column types across databases:
188
188
189
-
-`vector` - stores vectors as 4-byte floats (single precision)
190
-
- PostgreSQL: native `vector` type via pgvector extension
191
-
- SAP HANA: alias for `real_vector` type
192
-
-`halfvec` - stores vectors as 2-byte floats (half precision) for memory efficiency
193
-
- PostgreSQL: native `halfvec` type via pgvector extension
194
-
- SAP HANA: alias for `half_vector` type
189
+
-`vector` - stores vectors as 4-byte floats (single precision)
190
+
- PostgreSQL: native `vector` type via pgvector extension
191
+
- SQL Server: native `vector` type
192
+
- SAP HANA: alias for `real_vector` type
193
+
-`halfvec` - stores vectors as 2-byte floats (half precision) for memory efficiency
194
+
- PostgreSQL: native `halfvec` type via pgvector extension
195
+
- SAP HANA: alias for `half_vector` type
195
196
196
197
You can specify the vector dimensions using the `length` option:
197
198
@@ -201,45 +202,65 @@ export class Post {
201
202
@PrimaryGeneratedColumn()
202
203
id:number
203
204
204
-
// Vector without specified dimensions (works on PostgreSQL and SAP HANA)
205
+
// Vector without specified dimensions (works on PostgreSQL and SAP HANA; SQL Server requires explicit dimensions)
205
206
@Column("vector")
206
207
embedding:number[] |Buffer
207
208
208
-
// Vector with 3 dimensions: vector(3) (works on PostgreSQL and SAP HANA)
209
+
// Vector with 3 dimensions: vector(3)
209
210
@Column("vector", { length: 3 })
210
211
embedding_3d:number[] |Buffer
211
212
212
-
// Half-precision vector with 4 dimensions: halfvec(4) (works on PostgreSQL and SAP HANA)
213
+
// Half-precision vector with 4 dimensions: halfvec(4) (PostgreSQL and SAP HANA only)
213
214
@Column("halfvec", { length: 4 })
214
215
halfvec_embedding:number[] |Buffer
215
216
}
216
217
```
217
218
218
-
Vector columns can be used for similarity searches using PostgreSQL's vector operators:
219
+
**PostgreSQL** - Vector columns can be used for similarity searches using vector operators:
219
220
220
221
```typescript
221
222
// L2 distance (Euclidean) - <->
222
223
const results =awaitdataSource.query(
223
224
`SELECT id, embedding FROM post ORDER BY embedding <-> $1 LIMIT 5`,
224
-
["[1,2,3]"]
225
+
["[1,2,3]"],
225
226
)
226
227
227
228
// Cosine distance - <=>
228
229
const results =awaitdataSource.query(
229
230
`SELECT id, embedding FROM post ORDER BY embedding <=> $1 LIMIT 5`,
230
-
["[1,2,3]"]
231
+
["[1,2,3]"],
231
232
)
232
233
233
234
// Inner product - <#>
234
235
const results =awaitdataSource.query(
235
236
`SELECT id, embedding FROM post ORDER BY embedding <#> $1 LIMIT 5`,
236
-
["[1,2,3]"]
237
+
["[1,2,3]"],
237
238
)
238
239
```
239
240
240
-
> **Note**:
241
-
> -**PostgreSQL**: Vector columns require the `pgvector` extension to be installed. The extension provides the vector data types and similarity operators.
242
-
> -**SAP HANA**: Vector columns require SAP HANA Cloud (2024Q1+) and a supported version of `@sap/hana-client`. Use the appropriate [vector similarity functions](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/vector-functions) for similarity searches.
241
+
**SQL Server** - Use the `VECTOR_DISTANCE` function for similarity searches:
242
+
243
+
```typescript
244
+
const queryEmbedding = [1, 2, 3]
245
+
246
+
// Cosine distance
247
+
const results =awaitdataSource.query(
248
+
`
249
+
DECLARE @question AS VECTOR(3) = @0;
250
+
SELECT TOP (5) id, embedding,
251
+
VECTOR_DISTANCE('cosine', @question, embedding) AS distance
252
+
FROM post
253
+
ORDER BY VECTOR_DISTANCE('cosine', @question, embedding)
254
+
`,
255
+
[JSON.stringify(queryEmbedding)],
256
+
)
257
+
```
258
+
259
+
> **Note**:
260
+
>
261
+
> -**PostgreSQL**: Vector columns require the `pgvector` extension to be installed. The extension provides the vector data types and similarity operators.
262
+
> -**SQL Server**: Vector type support requires a compatible SQL Server version with vector functionality enabled.
263
+
> -**SAP HANA**: Vector columns require SAP HANA Cloud (2024Q1+) and a supported version of `@sap/hana-client`. Use the appropriate [vector similarity functions](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/vector-functions) for similarity searches.
0 commit comments