| 1 | [**@lancedb/lancedb**](../README.md) • **Docs** |
| 2 | |
| 3 | *** |
| 4 | |
| 5 | [@lancedb/lancedb](../globals.md) / VectorQuery |
| 6 | |
| 7 | # Class: VectorQuery |
| 8 | |
| 9 | A builder used to construct a vector search |
| 10 | |
| 11 | This 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 | |
| 65 | If this is called then any vector index is skipped |
| 66 | |
| 67 | An exhaustive (flat) search will be performed. The query vector will |
| 68 | be compared to every vector in the table. At high scales this can be |
| 69 | expensive. However, this is often still useful. For example, skipping |
| 70 | the vector index can give you ground truth results which you can use to |
| 71 | calculate 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 | |
| 83 | Set the vector column to query |
| 84 | |
| 85 | This controls which column is compared to the query vector supplied in |
| 86 | the 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 | |
| 100 | This parameter must be specified if the table has more than one column |
| 101 | whose data type is a fixed-size-list of floats. |
| 102 | |
| 103 | *** |
| 104 | |
| 105 | ### distanceType() |
| 106 | |
| 107 | > **distanceType**(`distanceType`): [`VectorQuery`](VectorQuery.md) |
| 108 | |
| 109 | Set the distance metric to use |
| 110 | |
| 111 | When performing a vector search we try and find the "nearest" vectors according |
| 112 | to some kind of distance metric. This parameter controls which distance metric to |
| 113 | use. 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 |
| 126 | distance metrics available. |
| 127 | |
| 128 | Note: if there is a vector index then the distance type used MUST match the distance |
| 129 | type used to train the vector index. If this is not done then the results will be |
| 130 | invalid. |
| 131 | |
| 132 | By 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 | |
| 158 | Execute 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 |
| 171 | of |
| 172 | - RecordBatch. |
| 173 | |
| 174 | By default, LanceDb will use many threads to calculate results and, when |
| 175 | the result set is large, multiple batches will be processed at one time. |
| 176 | This readahead is limited however and backpressure will be applied if this |
| 177 | stream is consumed slowly (this constrains the maximum memory used by a |
| 178 | single 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 | |
| 190 | Generates an explanation of the query execution plan. |
| 191 | |
| 192 | #### Parameters |
| 193 | |
| 194 | • **verbose**: `boolean` = `false` |
| 195 | |
| 196 | If true, provides a more detailed explanation. Defaults to false. |
| 197 | |
| 198 | #### Returns |
| 199 | |
| 200 | `Promise`<`string`> |
| 201 | |
| 202 | A Promise that resolves to a string containing the query execution plan explanation. |
| 203 | |
| 204 | #### Example |
| 205 | |
| 206 | ```ts |
| 207 | import * as lancedb from "@lancedb/lancedb" |
| 208 | const db = await lancedb.connect("./.lancedb"); |
| 209 | const table = await db.createTable("my_table", [ |
| 210 | { vector: [1.1, 0.9], id: "1" }, |
| 211 | ]); |
| 212 | const 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 | |
| 225 | A 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 | |
| 237 | where |
| 238 | |
| 239 | #### Deprecated |
| 240 | |
| 241 | Use `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 | |
| 253 | Set the maximum number of results to return. |
| 254 | |
| 255 | By default, a plain search has no limit. If this method is not |
| 256 | called 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 | |
| 294 | Set the number of partitions to search (probe) |
| 295 | |
| 296 | This argument is only used when the vector column has an IVF PQ index. |
| 297 | If there is no index then this value is ignored. |
| 298 | |
| 299 | The IVF stage of IVF PQ divides the input into partitions (clusters) of |
| 300 | related values. |
| 301 | |
| 302 | The partition whose centroids are closest to the query vector will be |
| 303 | exhaustiely searched to find matches. This parameter controls how many |
| 304 | partitions should be searched. |
| 305 | |
| 306 | Increasing this value will increase the recall of your query but will |
| 307 | also increase the latency of your query. The default value is 20. This |
| 308 | default is good for many cases but the best value to use will depend on |
| 309 | your data and the recall that you need to achieve. |
| 310 | |
| 311 | For best results we recommend tuning this parameter with a benchmark against |
| 312 | your actual data to find the smallest possible value that will still give |
| 313 | you 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 | |
| 329 | If this is called then filtering will happen after the vector search instead of |
| 330 | before. |
| 331 | |
| 332 | By default filtering will be performed before the vector search. This is how |
| 333 | filtering is typically understood to work. This prefilter step does add some |
| 334 | additional latency. Creating a scalar index on the filter column(s) can |
| 335 | often improve this latency. However, sometimes a filter is too complex or scalar |
| 336 | indices cannot be applied to the column. In these cases postfiltering can be |
| 337 | used instead of prefiltering to improve latency. |
| 338 | |
| 339 | Post filtering applies the filter to the results of the vector search. This means |
| 340 | we only run the filter on a much smaller set of data. However, it can cause the |
| 341 | query to return fewer than `limit` results (or even no results) if none of the nearest |
| 342 | results match the filter. |
| 343 | |
| 344 | Post 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 |
| 353 | factor 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 | |
| 361 | A multiplier to control how many additional rows are taken during the refine step |
| 362 | |
| 363 | This argument is only used when the vector column has an IVF PQ index. |
| 364 | If there is no index then this value is ignored. |
| 365 | |
| 366 | An IVF PQ index stores compressed (quantized) values. They query vector is compared |
| 367 | against these values and, since they are compressed, the comparison is inaccurate. |
| 368 | |
| 369 | This parameter can be used to refine the results. It can improve both improve recall |
| 370 | and correct the ordering of the nearest results. |
| 371 | |
| 372 | To 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 |
| 375 | then fetches the full, uncompressed, values for these 30 results. The results are |
| 376 | then reordered by the true distance and only the nearest 10 are kept. |
| 377 | |
| 378 | Note: there is a difference between calling this method with a value of 1 and never |
| 379 | calling this method at all. Calling this method with any value will have an impact |
| 380 | on your search latency. When you call this method with a `refine_factor` of 1 then |
| 381 | LanceDb still needs to fetch the full, uncompressed, values so that it can potentially |
| 382 | reorder the results. |
| 383 | |
| 384 | Note: if this method is NOT called then the distances returned in the _distance column |
| 385 | will be approximate distances based on the comparison of the quantized query vector |
| 386 | and the quantized result vectors. This can be considerably different than the true |
| 387 | distance 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 | |
| 403 | Return only the specified columns. |
| 404 | |
| 405 | By default a query will return all columns from the table. However, this can have |
| 406 | a very significant impact on latency. LanceDb stores data in a columnar fashion. This |
| 407 | means we can finely tune our I/O to select exactly the columns we need. |
| 408 | |
| 409 | As a best practice you should always limit queries to the columns that you need. If you |
| 410 | pass in an array of column names then only those columns will be returned. |
| 411 | |
| 412 | You can also use this method to create new "dynamic" columns based on your existing columns. |
| 413 | For example, you may not care about "a" or "b" but instead simply want "a + b". This is often |
| 414 | seen in the SELECT clause of an SQL query (e.g. `SELECT a+b FROM my_table`). |
| 415 | |
| 416 | To create dynamic columns you can pass in a Map<string, string>. A column will be returned |
| 417 | for each entry in the map. The key provides the name of the column. The value is |
| 418 | an SQL string used to specify how the column is calculated. |
| 419 | |
| 420 | For example, an SQL query might state `SELECT a + b AS combined, c`. The equivalent |
| 421 | input to this method would be: |
| 422 | |
| 423 | #### Parameters |
| 424 | |
| 425 | • **columns**: `string` \| `string`[] \| `Record`<`string`, `string`> \| `Map`<`string`, `string`> |
| 426 | |
| 427 | #### Returns |
| 428 | |
| 429 | `this` |
| 430 | |
| 431 | #### Example |
| 432 | |
| 433 | ```ts |
| 434 | new Map([["combined", "a + b"], ["c", "c"]]) |
| 435 | |
| 436 | Columns will always be returned in the order given, even if that order is different than |
| 437 | the order used when adding the data. |
| 438 | |
| 439 | Note that you can pass in a `Record<string, string>` (e.g. an object literal). This method |
| 440 | uses `Object.entries` which should preserve the insertion order of the object. However, |
| 441 | object 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`<`any`[]> |
| 453 | |
| 454 | Collect the results as an array of objects. |
| 455 | |
| 456 | #### Parameters |
| 457 | |
| 458 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 459 | |
| 460 | #### Returns |
| 461 | |
| 462 | `Promise`<`any`[]> |
| 463 | |
| 464 | #### Inherited from |
| 465 | |
| 466 | [`QueryBase`](QueryBase.md).[`toArray`](QueryBase.md#toarray) |
| 467 | |
| 468 | *** |
| 469 | |
| 470 | ### toArrow() |
| 471 | |
| 472 | > **toArrow**(`options`?): `Promise`<`Table`<`any`>> |
| 473 | |
| 474 | Collect the results as an Arrow |
| 475 | |
| 476 | #### Parameters |
| 477 | |
| 478 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 479 | |
| 480 | #### Returns |
| 481 | |
| 482 | `Promise`<`Table`<`any`>> |
| 483 | |
| 484 | #### See |
| 485 | |
| 486 | ArrowTable. |
| 487 | |
| 488 | #### Inherited from |
| 489 | |
| 490 | [`QueryBase`](QueryBase.md).[`toArrow`](QueryBase.md#toarrow) |
| 491 | |
| 492 | *** |
| 493 | |
| 494 | ### where() |
| 495 | |
| 496 | > **where**(`predicate`): `this` |
| 497 | |
| 498 | A filter statement to be applied to this query. |
| 499 | |
| 500 | The 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 |
| 513 | x > 10 |
| 514 | y > 0 AND y < 100 |
| 515 | x > 5 OR y = 'test' |
| 516 | |
| 517 | Filtering performance can often be improved by creating a scalar index |
| 518 | on the filter column(s). |
| 519 | ``` |
| 520 | |
| 521 | #### Inherited from |
| 522 | |
| 523 | [`QueryBase`](QueryBase.md).[`where`](QueryBase.md#where) |