Skip to content

Commit 6ec9860

Browse files
docs: Add docs for new types HalfVector, SparseVector and Bit (#293)
1 parent 16db824 commit 6ec9860

File tree

3 files changed

+109
-15
lines changed

3 files changed

+109
-15
lines changed

docs/06-concepts/02-models.md

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fields:
1818
employees: List<Employee>
1919
```
2020
21-
Supported types are [bool](https://api.dart.dev/dart-core/bool-class.html), [int](https://api.dart.dev/dart-core/int-class.html), [double](https://api.dart.dev/dart-core/double-class.html), [String](https://api.dart.dev/dart-core/String-class.html), [Duration](https://api.dart.dev/dart-core/Duration-class.html), [DateTime](https://api.dart.dev/dart-core/DateTime-class.html), [ByteData](https://api.dart.dev/dart-typed_data/ByteData-class.html), [UuidValue](https://pub.dev/documentation/uuid/latest/uuid_value/UuidValue-class.html), [Uri](https://api.dart.dev/dart-core/Uri-class.html), [BigInt](https://api.dart.dev/dart-core/BigInt-class.html), [Vector](#vector-fields) and other serializable [classes](#class), [exceptions](#exception) and [enums](#enum). You can also use [List](https://api.dart.dev/dart-core/List-class.html)s, [Map](https://api.dart.dev/dart-core/Map-class.html)s and [Set](https://api.dart.dev/dart-core/Set-class.html)s of the supported types, just make sure to specify the types. All supported types can also be used inside [Record](https://api.dart.dev/dart-core/Record-class.html)s. Null safety is supported. Once your classes are generated, you can use them as parameters or return types to endpoint methods.
21+
Supported types are [bool](https://api.dart.dev/dart-core/bool-class.html), [int](https://api.dart.dev/dart-core/int-class.html), [double](https://api.dart.dev/dart-core/double-class.html), [String](https://api.dart.dev/dart-core/String-class.html), [Duration](https://api.dart.dev/dart-core/Duration-class.html), [DateTime](https://api.dart.dev/dart-core/DateTime-class.html), [ByteData](https://api.dart.dev/dart-typed_data/ByteData-class.html), [UuidValue](https://pub.dev/documentation/uuid/latest/uuid_value/UuidValue-class.html), [Uri](https://api.dart.dev/dart-core/Uri-class.html), [BigInt](https://api.dart.dev/dart-core/BigInt-class.html), [Vector](#vector), [HalfVector](#halfvector), [SparseVector](#sparsevector), [Bit](#bit) and other serializable [classes](#class), [exceptions](#exception) and [enums](#enum). You can also use [List](https://api.dart.dev/dart-core/List-class.html)s, [Map](https://api.dart.dev/dart-core/Map-class.html)s and [Set](https://api.dart.dev/dart-core/Set-class.html)s of the supported types, just make sure to specify the types. All supported types can also be used inside [Record](https://api.dart.dev/dart-core/Record-class.html)s. Null safety is supported. Once your classes are generated, you can use them as parameters or return types to endpoint methods.
2222
2323
### Limiting visibility of a generated class
2424
@@ -135,7 +135,25 @@ fields:
135135

136136
## Vector fields
137137

138-
The `Vector` type is used for storing high-dimensional vectors, which are specially useful for similarity search operations.
138+
Vector types are used for storing high-dimensional vectors, which are especially useful for similarity search operations.
139+
140+
When specifying vector types, the dimension is required between parentheses (e.g., `Vector(1536)`). Common dimensions include:
141+
142+
- 1536 (OpenAI embeddings)
143+
- 768 (many sentence transformers)
144+
- 384 (smaller models)
145+
146+
All vector types support specialized distance operations for similarity search and filtering. See the [Vector distance operators](database/filter#vector-distance-operators) section for details.
147+
148+
To ensure optimal performance with vector similarity searches, consider creating specialized vector indexes on your vector fields. See the [Vector indexes](database/indexing#vector-indexes) section for more details.
149+
150+
:::info
151+
The usage of Vector fields requires the pgvector PostgreSQL extension to be installed, which comes by default on new Serverpod projects. To upgrade an existing project, see the [Upgrading to pgvector support](../upgrading/upgrade-to-pgvector) guide.
152+
:::
153+
154+
### Vector
155+
156+
The `Vector` type stores full-precision floating-point vectors for general-purpose embeddings.
139157

140158
```yaml
141159
class: Document
@@ -151,17 +169,44 @@ fields:
151169
embedding: Vector(1536)
152170
```
153171

154-
The number in parentheses specifies the vector dimensions. Common dimensions include:
172+
### HalfVector
155173

156-
- 1536 (OpenAI embeddings)
157-
- 768 (many sentence transformers)
158-
- 384 (smaller models)
174+
The `HalfVector` type uses half-precision (16-bit) floating-point numbers, providing memory savings with acceptable precision loss for several applications.
175+
176+
```yaml
177+
class: Document
178+
table: document
179+
fields:
180+
content: String
181+
### Half-precision embedding for memory efficiency
182+
embedding: HalfVector(1536)
183+
```
159184

160-
Vector fields support specialized distance operations for similarity search and filtering. See the [Vector distance operators](database/filter#vector-distance-operators) section for details.
185+
### SparseVector
161186

162-
:::info
163-
The usage of Vector fields requires the pgvector PostgreSQL extension to be installed, which comes by default on new Serverpod projects. To upgrade an existing project, see the [Upgrading to pgvector support](../upgrading/upgrade-to-pgvector) guide.
164-
:::
187+
The `SparseVector` type efficiently stores sparse vectors where most values are zero, which is ideal for high-dimensional data with few non-zero elements.
188+
189+
```yaml
190+
class: Document
191+
table: document
192+
fields:
193+
content: String
194+
### Sparse vector for keyword-based embeddings
195+
keywords: SparseVector(10000)
196+
```
197+
198+
### Bit
199+
200+
The `Bit` type stores binary vectors where each element is 0 or 1, offering maximum memory efficiency for binary embeddings.
201+
202+
```yaml
203+
class: Document
204+
table: document
205+
fields:
206+
content: String
207+
### Binary vector for semantic hashing
208+
hash: Bit(256)
209+
```
165210

166211
## Generated code
167212

docs/06-concepts/06-database/04-indexing.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ If no type is specified the default is `btree`. All [PostgreSQL index types](htt
6565

6666
### Vector indexes
6767

68-
To enhance the performance of vector similarity search, it is possible to create specialized vector indexes on `Vector` fields. Serverpod supports both `HNSW` and `IVFFLAT` index types with full parameter specification.
68+
To enhance the performance of vector similarity search, it is possible to create specialized vector indexes on vector fields (`Vector`, `HalfVector`, `SparseVector`, `Bit`). Serverpod supports both `hnsw` and `ivfflat` index types with full parameter specification.
6969

7070
:::info
71-
Each vector index can only be created on a single `Vector` field. It is not possible to create a vector index on multiple fields of any kind.
71+
Each vector index can only be created on a single vector field. It is not possible to create a vector index on multiple fields of any kind.
7272
:::
7373

7474
#### HNSW indexes
@@ -81,6 +81,8 @@ table: document
8181
fields:
8282
content: String
8383
embedding: Vector(1536)
84+
keywords: SparseVector(10000)
85+
hash: Bit(256)
8486
indexes:
8587
document_embedding_hnsw_idx:
8688
fields: embedding
@@ -89,11 +91,26 @@ indexes:
8991
parameters:
9092
m: 16
9193
ef_construction: 64
94+
document_keywords_idx:
95+
fields: keywords
96+
type: hnsw
97+
distanceFunction: innerProduct
98+
parameters:
99+
m: 16
100+
ef_construction: 64
101+
document_hash_idx:
102+
fields: hash
103+
type: hnsw
104+
distanceFunction: hamming
105+
parameters:
106+
m: 16
107+
ef_construction: 64
92108
```
93109

94110
Available HNSW parameters:
95-
- `m`: Maximum number of bi-directional links for each node (default: 16)
96-
- `efConstruction`: Size of the dynamic candidate list (default: 64)
111+
112+
- `m`: Maximum number of bidirectional links for each node (default: 16)
113+
- `ef_construction`: Size of the dynamic candidate list (default: 64)
97114

98115
#### IVFFLAT indexes
99116

@@ -115,6 +132,7 @@ indexes:
115132
```
116133

117134
Available IVFFLAT parameters:
135+
118136
- `lists`: Number of inverted lists (default: 100)
119137

120138
#### Distance functions
@@ -127,6 +145,14 @@ Supported distance functions for vector indexes (`distanceFunction` parameter):
127145
| `innerProduct` | Inner product | When vectors are normalized |
128146
| `cosine` | Cosine distance | Text embeddings |
129147
| `l1` | Manhattan or taxicab distance | Sparse/high-dimensional data |
148+
| `hamming` | Hamming distance | Binary vectors (Bit type) |
149+
| `jaccard` | Jaccard distance | Binary vectors (Bit type) |
150+
151+
Different vector types have specific limitations when creating indexes:
152+
153+
- **SparseVector**: Can only use HNSW indexes (IVFFLAT is not supported).
154+
- **HalfVector**: When using IVFFLAT indexes, the L1 distance function is not supported.
155+
- **Bit**: Only supports `hamming` (default) and `jaccard` distance functions.
130156

131157
:::tip
132158
If more than one distance function is going to be frequently used on the same vector field, consider creating one index for each distance function to ensure optimal performance.

docs/06-concepts/06-database/06-filter.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,25 @@ In the example we fetch all users that has a name that starts with A _or_ B.
211211

212212
### Vector distance operators
213213

214-
Vector fields support specialized distance operations for similarity search. Available vector distance operations:
214+
All vector field types support specialized distance operations for similarity search. Available vector distance operations:
215215

216+
**Vector, HalfVector, and SparseVector fields:**
216217
- `distanceL2` - Euclidean (L2) distance.
217218
- `distanceInnerProduct` - Inner product distance.
218219
- `distanceCosine` - Cosine distance.
219220
- `distanceL1` - Manhattan or taxicab (L1) distance.
220221

222+
**Bit vector fields:**
223+
- `distanceHamming` - Hamming distance.
224+
- `distanceJaccard` - Jaccard distance.
225+
221226
You can use vector distance operations with numeric comparisons for filtering and ordering:
222227

223228
```dart
224229
// The vector to compare against
225230
var queryVector = Vector([0.1, 0.2, 0.3, ...]);
231+
var sparseQuery = SparseVector([0.0, 1.0, 0.0, 2.5, ...]);
232+
var binaryQuery = Bit([1, 0, 1, 1, 0, ...]);
226233
227234
// Find top documents similar to a query vector
228235
var similarDocs = await Document.db.find(
@@ -232,6 +239,22 @@ var similarDocs = await Document.db.find(
232239
limit: 10,
233240
);
234241
242+
// Search using sparse vectors
243+
var keywordMatches = await Document.db.find(
244+
session,
245+
where: (t) => t.keywords.distanceInnerProduct(sparseQuery) < 0.3,
246+
orderBy: (t) => t.keywords.distanceInnerProduct(sparseQuery),
247+
limit: 5,
248+
);
249+
250+
// Search using binary vectors with Hamming distance
251+
var binaryMatches = await Document.db.find(
252+
session,
253+
where: (t) => t.hash.distanceHamming(binaryQuery) < 10,
254+
orderBy: (t) => t.hash.distanceHamming(binaryQuery),
255+
limit: 5,
256+
);
257+
235258
// Filter by distance range
236259
var mediumSimilarity = await Document.db.find(
237260
session,

0 commit comments

Comments
 (0)