| 1 | [**@lancedb/lancedb**](../README.md) • **Docs** |
| 2 | |
| 3 | *** |
| 4 | |
| 5 | [@lancedb/lancedb](../globals.md) / Query |
| 6 | |
| 7 | # Class: Query |
| 8 | |
| 9 | A 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 | |
| 81 | Execute 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 |
| 94 | of |
| 95 | - RecordBatch. |
| 96 | |
| 97 | By default, LanceDb will use many threads to calculate results and, when |
| 98 | the result set is large, multiple batches will be processed at one time. |
| 99 | This readahead is limited however and backpressure will be applied if this |
| 100 | stream is consumed slowly (this constrains the maximum memory used by a |
| 101 | single 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 | |
| 113 | Generates an explanation of the query execution plan. |
| 114 | |
| 115 | #### Parameters |
| 116 | |
| 117 | • **verbose**: `boolean` = `false` |
| 118 | |
| 119 | If true, provides a more detailed explanation. Defaults to false. |
| 120 | |
| 121 | #### Returns |
| 122 | |
| 123 | `Promise`<`string`> |
| 124 | |
| 125 | A Promise that resolves to a string containing the query execution plan explanation. |
| 126 | |
| 127 | #### Example |
| 128 | |
| 129 | ```ts |
| 130 | import * as lancedb from "@lancedb/lancedb" |
| 131 | const db = await lancedb.connect("./.lancedb"); |
| 132 | const table = await db.createTable("my_table", [ |
| 133 | { vector: [1.1, 0.9], id: "1" }, |
| 134 | ]); |
| 135 | const 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 | |
| 148 | A 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 | |
| 160 | where |
| 161 | |
| 162 | #### Deprecated |
| 163 | |
| 164 | Use `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 | |
| 176 | Set the maximum number of results to return. |
| 177 | |
| 178 | By default, a plain search has no limit. If this method is not |
| 179 | called 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 | |
| 217 | Find the nearest vectors to the given query vector. |
| 218 | |
| 219 | This converts the query from a plain query to a vector query. |
| 220 | |
| 221 | This method will attempt to convert the input to the query vector |
| 222 | expected by the embedding model. If the input cannot be converted |
| 223 | then an error will be thrown. |
| 224 | |
| 225 | By default, there is no embedding model, and the input should be |
| 226 | an array-like object of numbers (something that can be used as input |
| 227 | to Float32Array.from) |
| 228 | |
| 229 | If there is only one vector column (a column whose data type is a |
| 230 | fixed size list of floats) then the column does not need to be specified. |
| 231 | If 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 |
| 244 | to compare with. |
| 245 | |
| 246 | If no index has been created on the vector column then a vector query |
| 247 | will perform a distance comparison between the query vector and every |
| 248 | vector in the database and then sort the results. This is sometimes |
| 249 | called a "flat search" |
| 250 | |
| 251 | For small databases, with a few hundred thousand vectors or less, this can |
| 252 | be reasonably fast. In larger databases you should create a vector index |
| 253 | on the column. If there is a vector index then an "approximate" nearest |
| 254 | neighbor search (frequently called an ANN search) will be performed. This |
| 255 | search is much faster, but the results will be approximate. |
| 256 | |
| 257 | The query can be further parameterized using the returned builder. There |
| 258 | are various ANN search parameters that will let you fine tune your recall |
| 259 | accuracy vs search latency. |
| 260 | |
| 261 | Vector searches always have a `limit`. If `limit` has not been called then |
| 262 | a default `limit` of 10 will be used. |
| 263 | - [Query#limit](Query.md#limit) |
| 264 | |
| 265 | *** |
| 266 | |
| 267 | ### select() |
| 268 | |
| 269 | > **select**(`columns`): `this` |
| 270 | |
| 271 | Return only the specified columns. |
| 272 | |
| 273 | By default a query will return all columns from the table. However, this can have |
| 274 | a very significant impact on latency. LanceDb stores data in a columnar fashion. This |
| 275 | means we can finely tune our I/O to select exactly the columns we need. |
| 276 | |
| 277 | As a best practice you should always limit queries to the columns that you need. If you |
| 278 | pass in an array of column names then only those columns will be returned. |
| 279 | |
| 280 | You can also use this method to create new "dynamic" columns based on your existing columns. |
| 281 | For example, you may not care about "a" or "b" but instead simply want "a + b". This is often |
| 282 | seen in the SELECT clause of an SQL query (e.g. `SELECT a+b FROM my_table`). |
| 283 | |
| 284 | To create dynamic columns you can pass in a Map<string, string>. A column will be returned |
| 285 | for each entry in the map. The key provides the name of the column. The value is |
| 286 | an SQL string used to specify how the column is calculated. |
| 287 | |
| 288 | For example, an SQL query might state `SELECT a + b AS combined, c`. The equivalent |
| 289 | input to this method would be: |
| 290 | |
| 291 | #### Parameters |
| 292 | |
| 293 | • **columns**: `string` \| `string`[] \| `Record`<`string`, `string`> \| `Map`<`string`, `string`> |
| 294 | |
| 295 | #### Returns |
| 296 | |
| 297 | `this` |
| 298 | |
| 299 | #### Example |
| 300 | |
| 301 | ```ts |
| 302 | new Map([["combined", "a + b"], ["c", "c"]]) |
| 303 | |
| 304 | Columns will always be returned in the order given, even if that order is different than |
| 305 | the order used when adding the data. |
| 306 | |
| 307 | Note that you can pass in a `Record<string, string>` (e.g. an object literal). This method |
| 308 | uses `Object.entries` which should preserve the insertion order of the object. However, |
| 309 | object 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`<`any`[]> |
| 321 | |
| 322 | Collect the results as an array of objects. |
| 323 | |
| 324 | #### Parameters |
| 325 | |
| 326 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 327 | |
| 328 | #### Returns |
| 329 | |
| 330 | `Promise`<`any`[]> |
| 331 | |
| 332 | #### Inherited from |
| 333 | |
| 334 | [`QueryBase`](QueryBase.md).[`toArray`](QueryBase.md#toarray) |
| 335 | |
| 336 | *** |
| 337 | |
| 338 | ### toArrow() |
| 339 | |
| 340 | > **toArrow**(`options`?): `Promise`<`Table`<`any`>> |
| 341 | |
| 342 | Collect the results as an Arrow |
| 343 | |
| 344 | #### Parameters |
| 345 | |
| 346 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 347 | |
| 348 | #### Returns |
| 349 | |
| 350 | `Promise`<`Table`<`any`>> |
| 351 | |
| 352 | #### See |
| 353 | |
| 354 | ArrowTable. |
| 355 | |
| 356 | #### Inherited from |
| 357 | |
| 358 | [`QueryBase`](QueryBase.md).[`toArrow`](QueryBase.md#toarrow) |
| 359 | |
| 360 | *** |
| 361 | |
| 362 | ### where() |
| 363 | |
| 364 | > **where**(`predicate`): `this` |
| 365 | |
| 366 | A filter statement to be applied to this query. |
| 367 | |
| 368 | The 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 |
| 381 | x > 10 |
| 382 | y > 0 AND y < 100 |
| 383 | x > 5 OR y = 'test' |
| 384 | |
| 385 | Filtering performance can often be improved by creating a scalar index |
| 386 | on the filter column(s). |
| 387 | ``` |
| 388 | |
| 389 | #### Inherited from |
| 390 | |
| 391 | [`QueryBase`](QueryBase.md).[`where`](QueryBase.md#where) |