Elffuss BraidHub

kikocis/lancedb public

bbc8a2ecbe2c · 8.2 KiBRenderedRawPermalink
1[**@lancedb/lancedb**](../README.md) • **Docs**
2
3***
4
5[@lancedb/lancedb](../globals.md) / Query
6
7# Class: Query
8
9A builder for LanceDB queries.
10
11## Extends
12
13- [`QueryBase`](QueryBase.md)<`NativeQuery`>
14
15## Constructors
16
17### new Query()
18
19> **new Query**(`tbl`): [`Query`](Query.md)
20
21#### Parameters
22
23• **tbl**: `Table`
24
25#### Returns
26
27[`Query`](Query.md)
28
29#### Overrides
30
31[`QueryBase`](QueryBase.md).[`constructor`](QueryBase.md#constructors)
32
33## Properties
34
35### inner
36
37> `protected` **inner**: `Query` \| `Promise`<`Query`>
38
39#### Inherited from
40
41[`QueryBase`](QueryBase.md).[`inner`](QueryBase.md#inner)
42
43## Methods
44
45### \[asyncIterator\]()
46
47> **\[asyncIterator\]**(): `AsyncIterator`<`RecordBatch`<`any`>, `any`, `undefined`>
48
49#### Returns
50
51`AsyncIterator`<`RecordBatch`<`any`>, `any`, `undefined`>
52
53#### Inherited from
54
55[`QueryBase`](QueryBase.md).[`[asyncIterator]`](QueryBase.md#%5Basynciterator%5D)
56
57***
58
59### doCall()
60
61> `protected` **doCall**(`fn`): `void`
62
63#### Parameters
64
65• **fn**
66
67#### Returns
68
69`void`
70
71#### Inherited from
72
73[`QueryBase`](QueryBase.md).[`doCall`](QueryBase.md#docall)
74
75***
76
77### execute()
78
79> `protected` **execute**(`options`?): [`RecordBatchIterator`](RecordBatchIterator.md)
80
81Execute the query and return the results as an
82
83#### Parameters
84
85• **options?**: `Partial`<`QueryExecutionOptions`>
86
87#### Returns
88
89[`RecordBatchIterator`](RecordBatchIterator.md)
90
91#### See
92
93 - AsyncIterator
94of
95 - RecordBatch.
96
97By default, LanceDb will use many threads to calculate results and, when
98the result set is large, multiple batches will be processed at one time.
99This readahead is limited however and backpressure will be applied if this
100stream is consumed slowly (this constrains the maximum memory used by a
101single query)
102
103#### Inherited from
104
105[`QueryBase`](QueryBase.md).[`execute`](QueryBase.md#execute)
106
107***
108
109### explainPlan()
110
111> **explainPlan**(`verbose`): `Promise`<`string`>
112
113Generates an explanation of the query execution plan.
114
115#### Parameters
116
117• **verbose**: `boolean` = `false`
118
119If true, provides a more detailed explanation. Defaults to false.
120
121#### Returns
122
123`Promise`<`string`>
124
125A Promise that resolves to a string containing the query execution plan explanation.
126
127#### Example
128
129```ts
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], id: "1" },
134]);
135const plan = await table.query().nearestTo([0.5, 0.2]).explainPlan();
136```
137
138#### Inherited from
139
140[`QueryBase`](QueryBase.md).[`explainPlan`](QueryBase.md#explainplan)
141
142***
143
144### ~~filter()~~
145
146> **filter**(`predicate`): `this`
147
148A filter statement to be applied to this query.
149
150#### Parameters
151
152• **predicate**: `string`
153
154#### Returns
155
156`this`
157
158#### Alias
159
160where
161
162#### Deprecated
163
164Use `where` instead
165
166#### Inherited from
167
168[`QueryBase`](QueryBase.md).[`filter`](QueryBase.md#filter)
169
170***
171
172### limit()
173
174> **limit**(`limit`): `this`
175
176Set the maximum number of results to return.
177
178By default, a plain search has no limit. If this method is not
179called then every valid row from the table will be returned.
180
181#### Parameters
182
183• **limit**: `number`
184
185#### Returns
186
187`this`
188
189#### Inherited from
190
191[`QueryBase`](QueryBase.md).[`limit`](QueryBase.md#limit)
192
193***
194
195### nativeExecute()
196
197> `protected` **nativeExecute**(`options`?): `Promise`<`RecordBatchIterator`>
198
199#### Parameters
200
201• **options?**: `Partial`<`QueryExecutionOptions`>
202
203#### Returns
204
205`Promise`<`RecordBatchIterator`>
206
207#### Inherited from
208
209[`QueryBase`](QueryBase.md).[`nativeExecute`](QueryBase.md#nativeexecute)
210
211***
212
213### nearestTo()
214
215> **nearestTo**(`vector`): [`VectorQuery`](VectorQuery.md)
216
217Find the nearest vectors to the given query vector.
218
219This converts the query from a plain query to a vector query.
220
221This method will attempt to convert the input to the query vector
222expected by the embedding model. If the input cannot be converted
223then an error will be thrown.
224
225By default, there is no embedding model, and the input should be
226an array-like object of numbers (something that can be used as input
227to Float32Array.from)
228
229If there is only one vector column (a column whose data type is a
230fixed size list of floats) then the column does not need to be specified.
231If there is more than one vector column you must use
232
233#### Parameters
234
235• **vector**: `IntoVector`
236
237#### Returns
238
239[`VectorQuery`](VectorQuery.md)
240
241#### See
242
243 - [VectorQuery#column](VectorQuery.md#column) to specify which column you would like
244to compare with.
245
246If no index has been created on the vector column then a vector query
247will perform a distance comparison between the query vector and every
248vector in the database and then sort the results. This is sometimes
249called a "flat search"
250
251For small databases, with a few hundred thousand vectors or less, this can
252be reasonably fast. In larger databases you should create a vector index
253on the column. If there is a vector index then an "approximate" nearest
254neighbor search (frequently called an ANN search) will be performed. This
255search is much faster, but the results will be approximate.
256
257The query can be further parameterized using the returned builder. There
258are various ANN search parameters that will let you fine tune your recall
259accuracy vs search latency.
260
261Vector searches always have a `limit`. If `limit` has not been called then
262a default `limit` of 10 will be used.
263 - [Query#limit](Query.md#limit)
264
265***
266
267### select()
268
269> **select**(`columns`): `this`
270
271Return only the specified columns.
272
273By default a query will return all columns from the table. However, this can have
274a very significant impact on latency. LanceDb stores data in a columnar fashion. This
275means we can finely tune our I/O to select exactly the columns we need.
276
277As a best practice you should always limit queries to the columns that you need. If you
278pass in an array of column names then only those columns will be returned.
279
280You can also use this method to create new "dynamic" columns based on your existing columns.
281For example, you may not care about "a" or "b" but instead simply want "a + b". This is often
282seen in the SELECT clause of an SQL query (e.g. `SELECT a+b FROM my_table`).
283
284To create dynamic columns you can pass in a Map<string, string>. A column will be returned
285for each entry in the map. The key provides the name of the column. The value is
286an SQL string used to specify how the column is calculated.
287
288For example, an SQL query might state `SELECT a + b AS combined, c`. The equivalent
289input to this method would be:
290
291#### Parameters
292
293• **columns**: `string` \| `string`[] \| `Record`&lt;`string`, `string`&gt; \| `Map`&lt;`string`, `string`&gt;
294
295#### Returns
296
297`this`
298
299#### Example
300
301```ts
302new Map([["combined", "a + b"], ["c", "c"]])
303
304Columns will always be returned in the order given, even if that order is different than
305the order used when adding the data.
306
307Note that you can pass in a `Record<string, string>` (e.g. an object literal). This method
308uses `Object.entries` which should preserve the insertion order of the object. However,
309object insertion order is easy to get wrong and `Map` is more foolproof.
310```
311
312#### Inherited from
313
314[`QueryBase`](QueryBase.md).[`select`](QueryBase.md#select)
315
316***
317
318### toArray()
319
320> **toArray**(`options`?): `Promise`&lt;`any`[]&gt;
321
322Collect the results as an array of objects.
323
324#### Parameters
325
326• **options?**: `Partial`&lt;`QueryExecutionOptions`&gt;
327
328#### Returns
329
330`Promise`&lt;`any`[]&gt;
331
332#### Inherited from
333
334[`QueryBase`](QueryBase.md).[`toArray`](QueryBase.md#toarray)
335
336***
337
338### toArrow()
339
340> **toArrow**(`options`?): `Promise`&lt;`Table`&lt;`any`&gt;&gt;
341
342Collect the results as an Arrow
343
344#### Parameters
345
346• **options?**: `Partial`&lt;`QueryExecutionOptions`&gt;
347
348#### Returns
349
350`Promise`&lt;`Table`&lt;`any`&gt;&gt;
351
352#### See
353
354ArrowTable.
355
356#### Inherited from
357
358[`QueryBase`](QueryBase.md).[`toArrow`](QueryBase.md#toarrow)
359
360***
361
362### where()
363
364> **where**(`predicate`): `this`
365
366A filter statement to be applied to this query.
367
368The filter should be supplied as an SQL query string. For example:
369
370#### Parameters
371
372• **predicate**: `string`
373
374#### Returns
375
376`this`
377
378#### Example
379
380```ts
381x > 10
382y > 0 AND y < 100
383x > 5 OR y = 'test'
384
385Filtering performance can often be improved by creating a scalar index
386on the filter column(s).
387```
388
389#### Inherited from
390
391[`QueryBase`](QueryBase.md).[`where`](QueryBase.md#where)