| 1 | // --8<-- [start:import] |
| 2 | import * as vectordb from "vectordb"; |
| 3 | // --8<-- [end:import] |
| 4 | |
| 5 | (async () => { |
| 6 | // --8<-- [start:ingest] |
| 7 | const db = await vectordb.connect("data/sample-lancedb"); |
| 8 | |
| 9 | let data = []; |
| 10 | for (let i = 0; i < 10_000; i++) { |
| 11 | data.push({ |
| 12 | vector: Array(1536).fill(i), |
| 13 | id: `${i}`, |
| 14 | content: "", |
| 15 | longId: `${i}`, |
| 16 | }); |
| 17 | } |
| 18 | const table = await db.createTable("my_vectors", data); |
| 19 | await table.createIndex({ |
| 20 | type: "ivf_pq", |
| 21 | column: "vector", |
| 22 | num_partitions: 16, |
| 23 | num_sub_vectors: 48, |
| 24 | }); |
| 25 | // --8<-- [end:ingest] |
| 26 | |
| 27 | // --8<-- [start:search1] |
| 28 | const results_1 = await table |
| 29 | .search(Array(1536).fill(1.2)) |
| 30 | .limit(2) |
| 31 | .nprobes(20) |
| 32 | .refineFactor(10) |
| 33 | .execute(); |
| 34 | // --8<-- [end:search1] |
| 35 | |
| 36 | // --8<-- [start:search2] |
| 37 | const results_2 = await table |
| 38 | .search(Array(1536).fill(1.2)) |
| 39 | .where("id != '1141'") |
| 40 | .limit(2) |
| 41 | .execute(); |
| 42 | // --8<-- [end:search2] |
| 43 | |
| 44 | // --8<-- [start:search3] |
| 45 | const results_3 = await table |
| 46 | .search(Array(1536).fill(1.2)) |
| 47 | .select(["id"]) |
| 48 | .limit(2) |
| 49 | .execute(); |
| 50 | // --8<-- [end:search3] |
| 51 | |
| 52 | console.log("Ann indexes: done"); |
| 53 | })(); |