Skip to content

Commit 88e8e1e

Browse files
committed
Improved example
1 parent f6c17bf commit 88e8e1e

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ _ = try conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vecto
3030
Insert vectors
3131

3232
```zig
33-
const params = .{ "[1,2,3]", "[4,5,6]" };
34-
_ = try conn.exec("INSERT INTO items (embedding) VALUES ($1), ($2)", params);
33+
const embedding1 = [_]f32{ 1, 2, 3 };
34+
const embedding2 = [_]f32{ 4, 5, 6 };
35+
_ = try conn.exec("INSERT INTO items (embedding) VALUES ($1::float4[]::vector), ($2::float4[]::vector)", .{ embedding1, embedding2 });
3536
```
3637

3738
Get the nearest neighbors
3839

3940
```zig
40-
var result = try conn.query("SELECT id FROM items ORDER BY embedding <-> $1 LIMIT 5", .{"[3,1,2]"});
41+
const embedding3 = [_]f32{ 3, 1, 2 };
42+
var result = try conn.query("SELECT id FROM items ORDER BY embedding <-> $1::float4[]::vector LIMIT 5", .{embedding3});
4143
```
4244

4345
Add an approximate index

examples/pg.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ pub fn main() !void {
2121

2222
_ = try conn.exec("CREATE TABLE pg_items (id bigserial PRIMARY KEY, embedding vector(3))", .{});
2323

24-
const params = .{ "[1,1,1]", "[2,2,2]", "[1,1,2]" };
25-
_ = try conn.exec("INSERT INTO pg_items (embedding) VALUES ($1), ($2), ($3)", params);
24+
const params = .{ [_]f32{ 1, 1, 1 }, [_]f32{ 2, 2, 2 }, [_]f32{ 1, 1, 2 } };
25+
_ = try conn.exec("INSERT INTO pg_items (embedding) VALUES ($1::float4[]::vector), ($2::float4[]::vector), ($3::float4[]::vector)", params);
2626

27-
var result = try conn.query("SELECT id FROM pg_items ORDER BY embedding <-> $1 LIMIT 5", .{"[1,1,1]"});
27+
const embedding = [_]f32{ 3, 1, 2 };
28+
var result = try conn.query("SELECT id FROM pg_items ORDER BY embedding <-> $1::float4[]::vector LIMIT 5", .{embedding});
2829
defer result.deinit();
2930
while (try result.next()) |row| {
3031
const id = row.get(i64, 0);

0 commit comments

Comments
 (0)