| 1 | // --8<-- [end:import] |
| 2 | import * as fs from "node:fs"; |
| 3 | // --8<-- [start:import] |
| 4 | import * as lancedb from "@lancedb/lancedb"; |
| 5 | |
| 6 | async function setup() { |
| 7 | fs.rmSync("data/sample-lancedb", { recursive: true, force: true }); |
| 8 | const db = await lancedb.connect("data/sample-lancedb"); |
| 9 | |
| 10 | const data = Array.from({ length: 10_000 }, (_, i) => ({ |
| 11 | vector: Array(1536).fill(i), |
| 12 | id: `${i}`, |
| 13 | content: "", |
| 14 | longId: `${i}`, |
| 15 | })); |
| 16 | |
| 17 | await db.createTable("my_vectors", data); |
| 18 | } |
| 19 | |
| 20 | await setup(); |
| 21 | |
| 22 | // --8<-- [start:search1] |
| 23 | const db = await lancedb.connect("data/sample-lancedb"); |
| 24 | const tbl = await db.openTable("my_vectors"); |
| 25 | |
| 26 | const _results1 = await tbl.search(Array(1536).fill(1.2)).limit(10).toArray(); |
| 27 | // --8<-- [end:search1] |
| 28 | |
| 29 | // --8<-- [start:search2] |
| 30 | const _results2 = await tbl |
| 31 | .search(Array(1536).fill(1.2)) |
| 32 | .distanceType("cosine") |
| 33 | .limit(10) |
| 34 | .toArray(); |
| 35 | console.log(_results2); |
| 36 | // --8<-- [end:search2] |
| 37 | |
| 38 | console.log("search: done"); |