| 1 | // Copyright 2024 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import * as lancedb from "@lancedb/lancedb"; |
| 16 | |
| 17 | const db = await lancedb.connect("data/sample-lancedb"); |
| 18 | |
| 19 | const words = [ |
| 20 | "apple", |
| 21 | "banana", |
| 22 | "cherry", |
| 23 | "date", |
| 24 | "elderberry", |
| 25 | "fig", |
| 26 | "grape", |
| 27 | ]; |
| 28 | |
| 29 | const data = Array.from({ length: 10_000 }, (_, i) => ({ |
| 30 | vector: Array(1536).fill(i), |
| 31 | id: i, |
| 32 | item: `item ${i}`, |
| 33 | strId: `${i}`, |
| 34 | doc: words[i % words.length], |
| 35 | })); |
| 36 | |
| 37 | const tbl = await db.createTable("myVectors", data, { mode: "overwrite" }); |
| 38 | |
| 39 | await tbl.createIndex("doc", { |
| 40 | config: lancedb.Index.fts(), |
| 41 | }); |
| 42 | |
| 43 | // --8<-- [start:full_text_search] |
| 44 | let result = await tbl |
| 45 | .search("apple") |
| 46 | .select(["id", "doc"]) |
| 47 | .limit(10) |
| 48 | .toArray(); |
| 49 | console.log(result); |
| 50 | // --8<-- [end:full_text_search] |
| 51 | |
| 52 | console.log("SQL search: done"); |