| 1 | // --8<-- [start:import] |
| 2 | import * as lancedb from "@lancedb/lancedb"; |
| 3 | // --8<-- [end:import] |
| 4 | |
| 5 | // --8<-- [start:ingest] |
| 6 | const db = await lancedb.connect("/tmp/lancedb/"); |
| 7 | |
| 8 | const data = Array.from({ length: 10_000 }, (_, i) => ({ |
| 9 | vector: Array(1536).fill(i), |
| 10 | id: `${i}`, |
| 11 | content: "", |
| 12 | longId: `${i}`, |
| 13 | })); |
| 14 | |
| 15 | const table = await db.createTable("my_vectors", data, { mode: "overwrite" }); |
| 16 | await table.createIndex("vector", { |
| 17 | config: lancedb.Index.ivfPq({ |
| 18 | numPartitions: 16, |
| 19 | numSubVectors: 48, |
| 20 | }), |
| 21 | }); |
| 22 | // --8<-- [end:ingest] |
| 23 | |
| 24 | // --8<-- [start:search1] |
| 25 | const _results1 = await table |
| 26 | .search(Array(1536).fill(1.2)) |
| 27 | .limit(2) |
| 28 | .nprobes(20) |
| 29 | .refineFactor(10) |
| 30 | .toArray(); |
| 31 | // --8<-- [end:search1] |
| 32 | |
| 33 | // --8<-- [start:search2] |
| 34 | const _results2 = await table |
| 35 | .search(Array(1536).fill(1.2)) |
| 36 | .where("id != '1141'") |
| 37 | .limit(2) |
| 38 | .toArray(); |
| 39 | // --8<-- [end:search2] |
| 40 | |
| 41 | // --8<-- [start:search3] |
| 42 | const _results3 = await table |
| 43 | .search(Array(1536).fill(1.2)) |
| 44 | .select(["id"]) |
| 45 | .limit(2) |
| 46 | .toArray(); |
| 47 | // --8<-- [end:search3] |
| 48 | |
| 49 | console.log("Ann indexes: done"); |