Elffuss BraidHub

kikocis/lancedb public

main · 12.3 KiBRenderedRawPermalink
1[**@lancedb/lancedb**](../README.md) • **Docs**
2
3***
4
5[@lancedb/lancedb](../globals.md) / VectorQuery
6
7# Class: VectorQuery
8
9A builder used to construct a vector search
10
11This builder can be reused to execute the query many times.
12
13## Extends
14
15- [`QueryBase`](QueryBase.md)<`NativeVectorQuery`>
16
17## Constructors
18
19### new VectorQuery()
20
21> **new VectorQuery**(`inner`): [`VectorQuery`](VectorQuery.md)
22
23#### Parameters
24
25• **inner**: `VectorQuery` \| `Promise`<`VectorQuery`>
26
27#### Returns
28
29[`VectorQuery`](VectorQuery.md)
30
31#### Overrides
32
33[`QueryBase`](QueryBase.md).[`constructor`](QueryBase.md#constructors)
34
35## Properties
36
37### inner
38
39> `protected` **inner**: `VectorQuery` \| `Promise`<`VectorQuery`>
40
41#### Inherited from
42
43[`QueryBase`](QueryBase.md).[`inner`](QueryBase.md#inner)
44
45## Methods
46
47### \[asyncIterator\]()
48
49> **\[asyncIterator\]**(): `AsyncIterator`<`RecordBatch`<`any`>, `any`, `undefined`>
50
51#### Returns
52
53`AsyncIterator`<`RecordBatch`<`any`>, `any`, `undefined`>
54
55#### Inherited from
56
57[`QueryBase`](QueryBase.md).[`[asyncIterator]`](QueryBase.md#%5Basynciterator%5D)
58
59***
60
61### bypassVectorIndex()
62
63> **bypassVectorIndex**(): [`VectorQuery`](VectorQuery.md)
64
65If this is called then any vector index is skipped
66
67An exhaustive (flat) search will be performed. The query vector will
68be compared to every vector in the table. At high scales this can be
69expensive. However, this is often still useful. For example, skipping
70the vector index can give you ground truth results which you can use to
71calculate your recall to select an appropriate value for nprobes.
72
73#### Returns
74
75[`VectorQuery`](VectorQuery.md)
76
77***
78
79### column()
80
81> **column**(`column`): [`VectorQuery`](VectorQuery.md)
82
83Set the vector column to query
84
85This controls which column is compared to the query vector supplied in
86the call to
87
88#### Parameters
89
90• **column**: `string`
91
92#### Returns
93
94[`VectorQuery`](VectorQuery.md)
95
96#### See
97
98[Query#nearestTo](Query.md#nearestto)
99
100This parameter must be specified if the table has more than one column
101whose data type is a fixed-size-list of floats.
102
103***
104
105### distanceType()
106
107> **distanceType**(`distanceType`): [`VectorQuery`](VectorQuery.md)
108
109Set the distance metric to use
110
111When performing a vector search we try and find the "nearest" vectors according
112to some kind of distance metric. This parameter controls which distance metric to
113use. See
114
115#### Parameters
116
117• **distanceType**: `"l2"` \| `"cosine"` \| `"dot"`
118
119#### Returns
120
121[`VectorQuery`](VectorQuery.md)
122
123#### See
124
125[IvfPqOptions.distanceType](../interfaces/IvfPqOptions.md#distancetype) for more details on the different
126distance metrics available.
127
128Note: if there is a vector index then the distance type used MUST match the distance
129type used to train the vector index. If this is not done then the results will be
130invalid.
131
132By default "l2" is used.
133
134***
135
136### doCall()
137
138> `protected` **doCall**(`fn`): `void`
139
140#### Parameters
141
142• **fn**
143
144#### Returns
145
146`void`
147
148#### Inherited from
149
150[`QueryBase`](QueryBase.md).[`doCall`](QueryBase.md#docall)
151
152***
153
154### execute()
155
156> `protected` **execute**(`options`?): [`RecordBatchIterator`](RecordBatchIterator.md)
157
158Execute the query and return the results as an
159
160#### Parameters
161
162• **options?**: `Partial`<`QueryExecutionOptions`>
163
164#### Returns
165
166[`RecordBatchIterator`](RecordBatchIterator.md)
167
168#### See
169
170 - AsyncIterator
171of
172 - RecordBatch.
173
174By default, LanceDb will use many threads to calculate results and, when
175the result set is large, multiple batches will be processed at one time.
176This readahead is limited however and backpressure will be applied if this
177stream is consumed slowly (this constrains the maximum memory used by a
178single query)
179
180#### Inherited from
181
182[`QueryBase`](QueryBase.md).[`execute`](QueryBase.md#execute)
183
184***
185
186### explainPlan()
187
188> **explainPlan**(`verbose`): `Promise`<`string`>
189
190Generates an explanation of the query execution plan.
191
192#### Parameters
193
194• **verbose**: `boolean` = `false`
195
196If true, provides a more detailed explanation. Defaults to false.
197
198#### Returns
199
200`Promise`<`string`>
201
202A Promise that resolves to a string containing the query execution plan explanation.
203
204#### Example
205
206```ts
207import * as lancedb from "@lancedb/lancedb"
208const db = await lancedb.connect("./.lancedb");
209const table = await db.createTable("my_table", [
210 { vector: [1.1, 0.9], id: "1" },
211]);
212const plan = await table.query().nearestTo([0.5, 0.2]).explainPlan();
213```
214
215#### Inherited from
216
217[`QueryBase`](QueryBase.md).[`explainPlan`](QueryBase.md#explainplan)
218
219***
220
221### ~~filter()~~
222
223> **filter**(`predicate`): `this`
224
225A filter statement to be applied to this query.
226
227#### Parameters
228
229• **predicate**: `string`
230
231#### Returns
232
233`this`
234
235#### Alias
236
237where
238
239#### Deprecated
240
241Use `where` instead
242
243#### Inherited from
244
245[`QueryBase`](QueryBase.md).[`filter`](QueryBase.md#filter)
246
247***
248
249### limit()
250
251> **limit**(`limit`): `this`
252
253Set the maximum number of results to return.
254
255By default, a plain search has no limit. If this method is not
256called then every valid row from the table will be returned.
257
258#### Parameters
259
260• **limit**: `number`
261
262#### Returns
263
264`this`
265
266#### Inherited from
267
268[`QueryBase`](QueryBase.md).[`limit`](QueryBase.md#limit)
269
270***
271
272### nativeExecute()
273
274> `protected` **nativeExecute**(`options`?): `Promise`<`RecordBatchIterator`>
275
276#### Parameters
277
278• **options?**: `Partial`<`QueryExecutionOptions`>
279
280#### Returns
281
282`Promise`<`RecordBatchIterator`>
283
284#### Inherited from
285
286[`QueryBase`](QueryBase.md).[`nativeExecute`](QueryBase.md#nativeexecute)
287
288***
289
290### nprobes()
291
292> **nprobes**(`nprobes`): [`VectorQuery`](VectorQuery.md)
293
294Set the number of partitions to search (probe)
295
296This argument is only used when the vector column has an IVF PQ index.
297If there is no index then this value is ignored.
298
299The IVF stage of IVF PQ divides the input into partitions (clusters) of
300related values.
301
302The partition whose centroids are closest to the query vector will be
303exhaustiely searched to find matches. This parameter controls how many
304partitions should be searched.
305
306Increasing this value will increase the recall of your query but will
307also increase the latency of your query. The default value is 20. This
308default is good for many cases but the best value to use will depend on
309your data and the recall that you need to achieve.
310
311For best results we recommend tuning this parameter with a benchmark against
312your actual data to find the smallest possible value that will still give
313you the desired recall.
314
315#### Parameters
316
317• **nprobes**: `number`
318
319#### Returns
320
321[`VectorQuery`](VectorQuery.md)
322
323***
324
325### postfilter()
326
327> **postfilter**(): [`VectorQuery`](VectorQuery.md)
328
329If this is called then filtering will happen after the vector search instead of
330before.
331
332By default filtering will be performed before the vector search. This is how
333filtering is typically understood to work. This prefilter step does add some
334additional latency. Creating a scalar index on the filter column(s) can
335often improve this latency. However, sometimes a filter is too complex or scalar
336indices cannot be applied to the column. In these cases postfiltering can be
337used instead of prefiltering to improve latency.
338
339Post filtering applies the filter to the results of the vector search. This means
340we only run the filter on a much smaller set of data. However, it can cause the
341query to return fewer than `limit` results (or even no results) if none of the nearest
342results match the filter.
343
344Post filtering happens during the "refine stage" (described in more detail in
345
346#### Returns
347
348[`VectorQuery`](VectorQuery.md)
349
350#### See
351
352[VectorQuery#refineFactor](VectorQuery.md#refinefactor)). This means that setting a higher refine
353factor can often help restore some of the results lost by post filtering.
354
355***
356
357### refineFactor()
358
359> **refineFactor**(`refineFactor`): [`VectorQuery`](VectorQuery.md)
360
361A multiplier to control how many additional rows are taken during the refine step
362
363This argument is only used when the vector column has an IVF PQ index.
364If there is no index then this value is ignored.
365
366An IVF PQ index stores compressed (quantized) values. They query vector is compared
367against these values and, since they are compressed, the comparison is inaccurate.
368
369This parameter can be used to refine the results. It can improve both improve recall
370and correct the ordering of the nearest results.
371
372To refine results LanceDb will first perform an ANN search to find the nearest
373`limit` * `refine_factor` results. In other words, if `refine_factor` is 3 and
374`limit` is the default (10) then the first 30 results will be selected. LanceDb
375then fetches the full, uncompressed, values for these 30 results. The results are
376then reordered by the true distance and only the nearest 10 are kept.
377
378Note: there is a difference between calling this method with a value of 1 and never
379calling this method at all. Calling this method with any value will have an impact
380on your search latency. When you call this method with a `refine_factor` of 1 then
381LanceDb still needs to fetch the full, uncompressed, values so that it can potentially
382reorder the results.
383
384Note: if this method is NOT called then the distances returned in the _distance column
385will be approximate distances based on the comparison of the quantized query vector
386and the quantized result vectors. This can be considerably different than the true
387distance between the query vector and the actual uncompressed vector.
388
389#### Parameters
390
391• **refineFactor**: `number`
392
393#### Returns
394
395[`VectorQuery`](VectorQuery.md)
396
397***
398
399### select()
400
401> **select**(`columns`): `this`
402
403Return only the specified columns.
404
405By default a query will return all columns from the table. However, this can have
406a very significant impact on latency. LanceDb stores data in a columnar fashion. This
407means we can finely tune our I/O to select exactly the columns we need.
408
409As a best practice you should always limit queries to the columns that you need. If you
410pass in an array of column names then only those columns will be returned.
411
412You can also use this method to create new "dynamic" columns based on your existing columns.
413For example, you may not care about "a" or "b" but instead simply want "a + b". This is often
414seen in the SELECT clause of an SQL query (e.g. `SELECT a+b FROM my_table`).
415
416To create dynamic columns you can pass in a Map<string, string>. A column will be returned
417for each entry in the map. The key provides the name of the column. The value is
418an SQL string used to specify how the column is calculated.
419
420For example, an SQL query might state `SELECT a + b AS combined, c`. The equivalent
421input to this method would be:
422
423#### Parameters
424
425• **columns**: `string` \| `string`[] \| `Record`&lt;`string`, `string`&gt; \| `Map`&lt;`string`, `string`&gt;
426
427#### Returns
428
429`this`
430
431#### Example
432
433```ts
434new Map([["combined", "a + b"], ["c", "c"]])
435
436Columns will always be returned in the order given, even if that order is different than
437the order used when adding the data.
438
439Note that you can pass in a `Record<string, string>` (e.g. an object literal). This method
440uses `Object.entries` which should preserve the insertion order of the object. However,
441object insertion order is easy to get wrong and `Map` is more foolproof.
442```
443
444#### Inherited from
445
446[`QueryBase`](QueryBase.md).[`select`](QueryBase.md#select)
447
448***
449
450### toArray()
451
452> **toArray**(`options`?): `Promise`&lt;`any`[]&gt;
453
454Collect the results as an array of objects.
455
456#### Parameters
457
458• **options?**: `Partial`&lt;`QueryExecutionOptions`&gt;
459
460#### Returns
461
462`Promise`&lt;`any`[]&gt;
463
464#### Inherited from
465
466[`QueryBase`](QueryBase.md).[`toArray`](QueryBase.md#toarray)
467
468***
469
470### toArrow()
471
472> **toArrow**(`options`?): `Promise`&lt;`Table`&lt;`any`&gt;&gt;
473
474Collect the results as an Arrow
475
476#### Parameters
477
478• **options?**: `Partial`&lt;`QueryExecutionOptions`&gt;
479
480#### Returns
481
482`Promise`&lt;`Table`&lt;`any`&gt;&gt;
483
484#### See
485
486ArrowTable.
487
488#### Inherited from
489
490[`QueryBase`](QueryBase.md).[`toArrow`](QueryBase.md#toarrow)
491
492***
493
494### where()
495
496> **where**(`predicate`): `this`
497
498A filter statement to be applied to this query.
499
500The filter should be supplied as an SQL query string. For example:
501
502#### Parameters
503
504• **predicate**: `string`
505
506#### Returns
507
508`this`
509
510#### Example
511
512```ts
513x > 10
514y > 0 AND y < 100
515x > 5 OR y = 'test'
516
517Filtering performance can often be improved by creating a scalar index
518on the filter column(s).
519```
520
521#### Inherited from
522
523[`QueryBase`](QueryBase.md).[`where`](QueryBase.md#where)