Elffuss BraidHub

kikocis/lancedb public

85830ec79fd2 · 14.8 KiBRenderedRawPermalink
1[**@lancedb/lancedb**](../README.md) • **Docs**
2
3***
4
5[@lancedb/lancedb](../globals.md) / Table
6
7# Class: `abstract` Table
8
9A Table is a collection of Records in a LanceDB Database.
10
11A Table object is expected to be long lived and reused for multiple operations.
12Table objects will cache a certain amount of index data in memory. This cache
13will be freed when the Table is garbage collected. To eagerly free the cache you
14can call the `close` method. Once the Table is closed, it cannot be used for any
15further operations.
16
17Closing a table is optional. It not closed, it will be closed when it is garbage
18collected.
19
20## Constructors
21
22### new Table()
23
24> **new Table**(): [`Table`](Table.md)
25
26#### Returns
27
28[`Table`](Table.md)
29
30## Accessors
31
32### name
33
34> `get` `abstract` **name**(): `string`
35
36Returns the name of the table
37
38#### Returns
39
40`string`
41
42## Methods
43
44### add()
45
46> `abstract` **add**(`data`, `options`?): `Promise`<`void`>
47
48Insert records into this Table.
49
50#### Parameters
51
52• **data**: [`Data`](../type-aliases/Data.md)
53
54Records to be inserted into the Table
55
56• **options?**: `Partial`<[`AddDataOptions`](../interfaces/AddDataOptions.md)>
57
58#### Returns
59
60`Promise`<`void`>
61
62***
63
64### addColumns()
65
66> `abstract` **addColumns**(`newColumnTransforms`): `Promise`<`void`>
67
68Add new columns with defined values.
69
70#### Parameters
71
72• **newColumnTransforms**: [`AddColumnsSql`](../interfaces/AddColumnsSql.md)[]
73
74pairs of column names and
75the SQL expression to use to calculate the value of the new column. These
76expressions will be evaluated for each row in the table, and can
77reference existing columns in the table.
78
79#### Returns
80
81`Promise`<`void`>
82
83***
84
85### alterColumns()
86
87> `abstract` **alterColumns**(`columnAlterations`): `Promise`<`void`>
88
89Alter the name or nullability of columns.
90
91#### Parameters
92
93• **columnAlterations**: [`ColumnAlteration`](../interfaces/ColumnAlteration.md)[]
94
95One or more alterations to
96apply to columns.
97
98#### Returns
99
100`Promise`<`void`>
101
102***
103
104### checkout()
105
106> `abstract` **checkout**(`version`): `Promise`<`void`>
107
108Checks out a specific version of the table _This is an in-place operation._
109
110This allows viewing previous versions of the table. If you wish to
111keep writing to the dataset starting from an old version, then use
112the `restore` function.
113
114Calling this method will set the table into time-travel mode. If you
115wish to return to standard mode, call `checkoutLatest`.
116
117#### Parameters
118
119• **version**: `number`
120
121The version to checkout
122
123#### Returns
124
125`Promise`<`void`>
126
127#### Example
128
129```typescript
130import * as lancedb from "@lancedb/lancedb"
131const db = await lancedb.connect("./.lancedb");
132const table = await db.createTable("my_table", [
133 { vector: [1.1, 0.9], type: "vector" },
134]);
135
136console.log(await table.version()); // 1
137console.log(table.display());
138await table.add([{ vector: [0.5, 0.2], type: "vector" }]);
139await table.checkout(1);
140console.log(await table.version()); // 2
141```
142
143***
144
145### checkoutLatest()
146
147> `abstract` **checkoutLatest**(): `Promise`<`void`>
148
149Checkout the latest version of the table. _This is an in-place operation._
150
151The table will be set back into standard mode, and will track the latest
152version of the table.
153
154#### Returns
155
156`Promise`<`void`>
157
158***
159
160### close()
161
162> `abstract` **close**(): `void`
163
164Close the table, releasing any underlying resources.
165
166It is safe to call this method multiple times.
167
168Any attempt to use the table after it is closed will result in an error.
169
170#### Returns
171
172`void`
173
174***
175
176### countRows()
177
178> `abstract` **countRows**(`filter`?): `Promise`<`number`>
179
180Count the total number of rows in the dataset.
181
182#### Parameters
183
184• **filter?**: `string`
185
186#### Returns
187
188`Promise`<`number`>
189
190***
191
192### createIndex()
193
194> `abstract` **createIndex**(`column`, `options`?): `Promise`<`void`>
195
196Create an index to speed up queries.
197
198Indices can be created on vector columns or scalar columns.
199Indices on vector columns will speed up vector searches.
200Indices on scalar columns will speed up filtering (in both
201vector and non-vector searches)
202
203#### Parameters
204
205• **column**: `string`
206
207• **options?**: `Partial`<[`IndexOptions`](../interfaces/IndexOptions.md)>
208
209#### Returns
210
211`Promise`<`void`>
212
213#### Note
214
215We currently don't support custom named indexes,
216The index name will always be `${column}_idx`
217
218#### Examples
219
220```ts
221// If the column has a vector (fixed size list) data type then
222// an IvfPq vector index will be created.
223const table = await conn.openTable("my_table");
224await table.createIndex("vector");
225```
226
227```ts
228// For advanced control over vector index creation you can specify
229// the index type and options.
230const table = await conn.openTable("my_table");
231await table.createIndex("vector", {
232 config: lancedb.Index.ivfPq({
233 numPartitions: 128,
234 numSubVectors: 16,
235 }),
236});
237```
238
239```ts
240// Or create a Scalar index
241await table.createIndex("my_float_col");
242```
243
244***
245
246### delete()
247
248> `abstract` **delete**(`predicate`): `Promise`<`void`>
249
250Delete the rows that satisfy the predicate.
251
252#### Parameters
253
254• **predicate**: `string`
255
256#### Returns
257
258`Promise`<`void`>
259
260***
261
262### display()
263
264> `abstract` **display**(): `string`
265
266Return a brief description of the table
267
268#### Returns
269
270`string`
271
272***
273
274### dropColumns()
275
276> `abstract` **dropColumns**(`columnNames`): `Promise`<`void`>
277
278Drop one or more columns from the dataset
279
280This is a metadata-only operation and does not remove the data from the
281underlying storage. In order to remove the data, you must subsequently
282call ``compact_files`` to rewrite the data without the removed columns and
283then call ``cleanup_files`` to remove the old files.
284
285#### Parameters
286
287• **columnNames**: `string`[]
288
289The names of the columns to drop. These can
290be nested column references (e.g. "a.b.c") or top-level column names
291(e.g. "a").
292
293#### Returns
294
295`Promise`<`void`>
296
297***
298
299### indexStats()
300
301> `abstract` **indexStats**(`name`): `Promise`<`undefined` \| [`IndexStatistics`](../interfaces/IndexStatistics.md)>
302
303List all the stats of a specified index
304
305#### Parameters
306
307• **name**: `string`
308
309The name of the index.
310
311#### Returns
312
313`Promise`<`undefined` \| [`IndexStatistics`](../interfaces/IndexStatistics.md)>
314
315The stats of the index. If the index does not exist, it will return undefined
316
317***
318
319### isOpen()
320
321> `abstract` **isOpen**(): `boolean`
322
323Return true if the table has not been closed
324
325#### Returns
326
327`boolean`
328
329***
330
331### listIndices()
332
333> `abstract` **listIndices**(): `Promise`<[`IndexConfig`](../interfaces/IndexConfig.md)[]>
334
335List all indices that have been created with [Table.createIndex](Table.md#createindex)
336
337#### Returns
338
339`Promise`<[`IndexConfig`](../interfaces/IndexConfig.md)[]>
340
341***
342
343### mergeInsert()
344
345> `abstract` **mergeInsert**(`on`): `MergeInsertBuilder`
346
347#### Parameters
348
349• **on**: `string` \| `string`[]
350
351#### Returns
352
353`MergeInsertBuilder`
354
355***
356
357### optimize()
358
359> `abstract` **optimize**(`options`?): `Promise`<`OptimizeStats`>
360
361Optimize the on-disk data and indices for better performance.
362
363Modeled after ``VACUUM`` in PostgreSQL.
364
365 Optimization covers three operations:
366
367 - Compaction: Merges small files into larger ones
368 - Prune: Removes old versions of the dataset
369 - Index: Optimizes the indices, adding new data to existing indices
370
371 Experimental API
372 ----------------
373
374 The optimization process is undergoing active development and may change.
375 Our goal with these changes is to improve the performance of optimization and
376 reduce the complexity.
377
378 That being said, it is essential today to run optimize if you want the best
379 performance. It should be stable and safe to use in production, but it our
380 hope that the API may be simplified (or not even need to be called) in the
381 future.
382
383 The frequency an application shoudl call optimize is based on the frequency of
384 data modifications. If data is frequently added, deleted, or updated then
385 optimize should be run frequently. A good rule of thumb is to run optimize if
386 you have added or modified 100,000 or more records or run more than 20 data
387 modification operations.
388
389#### Parameters
390
391• **options?**: `Partial`<`OptimizeOptions`>
392
393#### Returns
394
395`Promise`<`OptimizeStats`>
396
397***
398
399### query()
400
401> `abstract` **query**(): [`Query`](Query.md)
402
403Create a [Query](Query.md) Builder.
404
405Queries allow you to search your existing data. By default the query will
406return all the data in the table in no particular order. The builder
407returned by this method can be used to control the query using filtering,
408vector similarity, sorting, and more.
409
410Note: By default, all columns are returned. For best performance, you should
411only fetch the columns you need.
412
413When appropriate, various indices and statistics based pruning will be used to
414accelerate the query.
415
416#### Returns
417
418[`Query`](Query.md)
419
420A builder that can be used to parameterize the query
421
422#### Examples
423
424```ts
425// SQL-style filtering
426//
427// This query will return up to 1000 rows whose value in the `id` column
428// is greater than 5. LanceDb supports a broad set of filtering functions.
429for await (const batch of table
430 .query()
431 .where("id > 1")
432 .select(["id"])
433 .limit(20)) {
434 console.log(batch);
435}
436```
437
438```ts
439// Vector Similarity Search
440//
441// This example will find the 10 rows whose value in the "vector" column are
442// closest to the query vector [1.0, 2.0, 3.0]. If an index has been created
443// on the "vector" column then this will perform an ANN search.
444//
445// The `refineFactor` and `nprobes` methods are used to control the recall /
446// latency tradeoff of the search.
447for await (const batch of table
448 .query()
449 .where("id > 1")
450 .select(["id"])
451 .limit(20)) {
452 console.log(batch);
453}
454```
455
456```ts
457// Scan the full dataset
458//
459// This query will return everything in the table in no particular order.
460for await (const batch of table.query()) {
461 console.log(batch);
462}
463```
464
465***
466
467### restore()
468
469> `abstract` **restore**(): `Promise`<`void`>
470
471Restore the table to the currently checked out version
472
473This operation will fail if checkout has not been called previously
474
475This operation will overwrite the latest version of the table with a
476previous version. Any changes made since the checked out version will
477no longer be visible.
478
479Once the operation concludes the table will no longer be in a checked
480out state and the read_consistency_interval, if any, will apply.
481
482#### Returns
483
484`Promise`<`void`>
485
486***
487
488### schema()
489
490> `abstract` **schema**(): `Promise`<`Schema`<`any`>>
491
492Get the schema of the table.
493
494#### Returns
495
496`Promise`<`Schema`<`any`>>
497
498***
499
500### search()
501
502#### search(query)
503
504> `abstract` **search**(`query`, `queryType`, `ftsColumns`): [`VectorQuery`](VectorQuery.md)
505
506Create a search query to find the nearest neighbors
507of the given query vector, or the documents
508with the highest relevance to the query string.
509
510##### Parameters
511
512• **query**: `string`
513
514the query. This will be converted to a vector using the table's provided embedding function,
515or the query string for full-text search if `queryType` is "fts".
516
517• **queryType**: `string` = `"auto"` \| `"fts"`
518
519the type of query to run. If "auto", the query type will be determined based on the query.
520
521• **ftsColumns**: `string[] | str` = undefined
522
523the columns to search in. If not provided, all indexed columns will be searched.
524
525For now, this can support to search only one column.
526
527##### Returns
528
529[`VectorQuery`](VectorQuery.md)
530
531##### Note
532
533If no embedding functions are defined in the table, this will error when collecting the results.
534
535#### search(query)
536
537> `abstract` **search**(`query`): [`VectorQuery`](VectorQuery.md)
538
539Create a search query to find the nearest neighbors
540of the given query vector
541
542##### Parameters
543
544• **query**: `IntoVector`
545
546the query vector
547
548##### Returns
549
550[`VectorQuery`](VectorQuery.md)
551
552***
553
554### toArrow()
555
556> `abstract` **toArrow**(): `Promise`<`Table`<`any`>>
557
558Return the table as an arrow table
559
560#### Returns
561
562`Promise`<`Table`<`any`>>
563
564***
565
566### update()
567
568#### update(opts)
569
570> `abstract` **update**(`opts`): `Promise`<`void`>
571
572Update existing records in the Table
573
574##### Parameters
575
576• **opts**: `object` & `Partial`<[`UpdateOptions`](../interfaces/UpdateOptions.md)>
577
578##### Returns
579
580`Promise`<`void`>
581
582##### Example
583
584```ts
585table.update({where:"x = 2", values:{"vector": [10, 10]}})
586```
587
588#### update(opts)
589
590> `abstract` **update**(`opts`): `Promise`<`void`>
591
592Update existing records in the Table
593
594##### Parameters
595
596• **opts**: `object` & `Partial`<[`UpdateOptions`](../interfaces/UpdateOptions.md)>
597
598##### Returns
599
600`Promise`<`void`>
601
602##### Example
603
604```ts
605table.update({where:"x = 2", valuesSql:{"x": "x + 1"}})
606```
607
608#### update(updates, options)
609
610> `abstract` **update**(`updates`, `options`?): `Promise`<`void`>
611
612Update existing records in the Table
613
614An update operation can be used to adjust existing values. Use the
615returned builder to specify which columns to update. The new value
616can be a literal value (e.g. replacing nulls with some default value)
617or an expression applied to the old value (e.g. incrementing a value)
618
619An optional condition can be specified (e.g. "only update if the old
620value is 0")
621
622Note: if your condition is something like "some_id_column == 7" and
623you are updating many rows (with different ids) then you will get
624better performance with a single [`merge_insert`] call instead of
625repeatedly calilng this method.
626
627##### Parameters
628
629• **updates**: `Record`<`string`, `string`> \| `Map`<`string`, `string`>
630
631the
632columns to update
633
634Keys in the map should specify the name of the column to update.
635Values in the map provide the new value of the column. These can
636be SQL literal strings (e.g. "7" or "'foo'") or they can be expressions
637based on the row being updated (e.g. "my_col + 1")
638
639• **options?**: `Partial`<[`UpdateOptions`](../interfaces/UpdateOptions.md)>
640
641additional options to control
642the update behavior
643
644##### Returns
645
646`Promise`<`void`>
647
648***
649
650### vectorSearch()
651
652> `abstract` **vectorSearch**(`vector`): [`VectorQuery`](VectorQuery.md)
653
654Search the table with a given query vector.
655
656This is a convenience method for preparing a vector query and
657is the same thing as calling `nearestTo` on the builder returned
658by `query`.
659
660#### Parameters
661
662• **vector**: `IntoVector`
663
664#### Returns
665
666[`VectorQuery`](VectorQuery.md)
667
668#### See
669
670[Query#nearestTo](Query.md#nearestto) for more details.
671
672***
673
674### version()
675
676> `abstract` **version**(): `Promise`<`number`>
677
678Retrieve the version of the table
679
680#### Returns
681
682`Promise`<`number`>
683
684***
685
686### parseTableData()
687
688> `static` **parseTableData**(`data`, `options`?, `streaming`?): `Promise`<`object`>
689
690#### Parameters
691
692• **data**: `TableLike` \| `Record`<`string`, `unknown`>[]
693
694• **options?**: `Partial`<[`CreateTableOptions`](../interfaces/CreateTableOptions.md)>
695
696• **streaming?**: `boolean` = `false`
697
698#### Returns
699
700`Promise`<`object`>
701
702##### buf
703
704> **buf**: `Buffer`
705
706##### mode
707
708> **mode**: `string`