| 1 | [**@lancedb/lancedb**](../README.md) • **Docs** |
| 2 | |
| 3 | *** |
| 4 | |
| 5 | [@lancedb/lancedb](../globals.md) / QueryBase |
| 6 | |
| 7 | # Class: QueryBase<NativeQueryType> |
| 8 | |
| 9 | Common methods supported by all query types |
| 10 | |
| 11 | ## Extended by |
| 12 | |
| 13 | - [`Query`](Query.md) |
| 14 | - [`VectorQuery`](VectorQuery.md) |
| 15 | |
| 16 | ## Type Parameters |
| 17 | |
| 18 | • **NativeQueryType** *extends* `NativeQuery` \| `NativeVectorQuery` |
| 19 | |
| 20 | ## Implements |
| 21 | |
| 22 | - `AsyncIterable`<`RecordBatch`> |
| 23 | |
| 24 | ## Constructors |
| 25 | |
| 26 | ### new QueryBase() |
| 27 | |
| 28 | > `protected` **new QueryBase**<`NativeQueryType`>(`inner`): [`QueryBase`](QueryBase.md)<`NativeQueryType`> |
| 29 | |
| 30 | #### Parameters |
| 31 | |
| 32 | • **inner**: `NativeQueryType` \| `Promise`<`NativeQueryType`> |
| 33 | |
| 34 | #### Returns |
| 35 | |
| 36 | [`QueryBase`](QueryBase.md)<`NativeQueryType`> |
| 37 | |
| 38 | ## Properties |
| 39 | |
| 40 | ### inner |
| 41 | |
| 42 | > `protected` **inner**: `NativeQueryType` \| `Promise`<`NativeQueryType`> |
| 43 | |
| 44 | ## Methods |
| 45 | |
| 46 | ### \[asyncIterator\]() |
| 47 | |
| 48 | > **\[asyncIterator\]**(): `AsyncIterator`<`RecordBatch`<`any`>, `any`, `undefined`> |
| 49 | |
| 50 | #### Returns |
| 51 | |
| 52 | `AsyncIterator`<`RecordBatch`<`any`>, `any`, `undefined`> |
| 53 | |
| 54 | #### Implementation of |
| 55 | |
| 56 | `AsyncIterable.[asyncIterator]` |
| 57 | |
| 58 | *** |
| 59 | |
| 60 | ### doCall() |
| 61 | |
| 62 | > `protected` **doCall**(`fn`): `void` |
| 63 | |
| 64 | #### Parameters |
| 65 | |
| 66 | • **fn** |
| 67 | |
| 68 | #### Returns |
| 69 | |
| 70 | `void` |
| 71 | |
| 72 | *** |
| 73 | |
| 74 | ### execute() |
| 75 | |
| 76 | > `protected` **execute**(`options`?): [`RecordBatchIterator`](RecordBatchIterator.md) |
| 77 | |
| 78 | Execute the query and return the results as an |
| 79 | |
| 80 | #### Parameters |
| 81 | |
| 82 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 83 | |
| 84 | #### Returns |
| 85 | |
| 86 | [`RecordBatchIterator`](RecordBatchIterator.md) |
| 87 | |
| 88 | #### See |
| 89 | |
| 90 | - AsyncIterator |
| 91 | of |
| 92 | - RecordBatch. |
| 93 | |
| 94 | By default, LanceDb will use many threads to calculate results and, when |
| 95 | the result set is large, multiple batches will be processed at one time. |
| 96 | This readahead is limited however and backpressure will be applied if this |
| 97 | stream is consumed slowly (this constrains the maximum memory used by a |
| 98 | single query) |
| 99 | |
| 100 | *** |
| 101 | |
| 102 | ### explainPlan() |
| 103 | |
| 104 | > **explainPlan**(`verbose`): `Promise`<`string`> |
| 105 | |
| 106 | Generates an explanation of the query execution plan. |
| 107 | |
| 108 | #### Parameters |
| 109 | |
| 110 | • **verbose**: `boolean` = `false` |
| 111 | |
| 112 | If true, provides a more detailed explanation. Defaults to false. |
| 113 | |
| 114 | #### Returns |
| 115 | |
| 116 | `Promise`<`string`> |
| 117 | |
| 118 | A Promise that resolves to a string containing the query execution plan explanation. |
| 119 | |
| 120 | #### Example |
| 121 | |
| 122 | ```ts |
| 123 | import * as lancedb from "@lancedb/lancedb" |
| 124 | const db = await lancedb.connect("./.lancedb"); |
| 125 | const table = await db.createTable("my_table", [ |
| 126 | { vector: [1.1, 0.9], id: "1" }, |
| 127 | ]); |
| 128 | const plan = await table.query().nearestTo([0.5, 0.2]).explainPlan(); |
| 129 | ``` |
| 130 | |
| 131 | *** |
| 132 | |
| 133 | ### ~~filter()~~ |
| 134 | |
| 135 | > **filter**(`predicate`): `this` |
| 136 | |
| 137 | A filter statement to be applied to this query. |
| 138 | |
| 139 | #### Parameters |
| 140 | |
| 141 | • **predicate**: `string` |
| 142 | |
| 143 | #### Returns |
| 144 | |
| 145 | `this` |
| 146 | |
| 147 | #### Alias |
| 148 | |
| 149 | where |
| 150 | |
| 151 | #### Deprecated |
| 152 | |
| 153 | Use `where` instead |
| 154 | |
| 155 | *** |
| 156 | |
| 157 | ### limit() |
| 158 | |
| 159 | > **limit**(`limit`): `this` |
| 160 | |
| 161 | Set the maximum number of results to return. |
| 162 | |
| 163 | By default, a plain search has no limit. If this method is not |
| 164 | called then every valid row from the table will be returned. |
| 165 | |
| 166 | #### Parameters |
| 167 | |
| 168 | • **limit**: `number` |
| 169 | |
| 170 | #### Returns |
| 171 | |
| 172 | `this` |
| 173 | |
| 174 | *** |
| 175 | |
| 176 | ### nativeExecute() |
| 177 | |
| 178 | > `protected` **nativeExecute**(`options`?): `Promise`<`RecordBatchIterator`> |
| 179 | |
| 180 | #### Parameters |
| 181 | |
| 182 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 183 | |
| 184 | #### Returns |
| 185 | |
| 186 | `Promise`<`RecordBatchIterator`> |
| 187 | |
| 188 | *** |
| 189 | |
| 190 | ### select() |
| 191 | |
| 192 | > **select**(`columns`): `this` |
| 193 | |
| 194 | Return only the specified columns. |
| 195 | |
| 196 | By default a query will return all columns from the table. However, this can have |
| 197 | a very significant impact on latency. LanceDb stores data in a columnar fashion. This |
| 198 | means we can finely tune our I/O to select exactly the columns we need. |
| 199 | |
| 200 | As a best practice you should always limit queries to the columns that you need. If you |
| 201 | pass in an array of column names then only those columns will be returned. |
| 202 | |
| 203 | You can also use this method to create new "dynamic" columns based on your existing columns. |
| 204 | For example, you may not care about "a" or "b" but instead simply want "a + b". This is often |
| 205 | seen in the SELECT clause of an SQL query (e.g. `SELECT a+b FROM my_table`). |
| 206 | |
| 207 | To create dynamic columns you can pass in a Map<string, string>. A column will be returned |
| 208 | for each entry in the map. The key provides the name of the column. The value is |
| 209 | an SQL string used to specify how the column is calculated. |
| 210 | |
| 211 | For example, an SQL query might state `SELECT a + b AS combined, c`. The equivalent |
| 212 | input to this method would be: |
| 213 | |
| 214 | #### Parameters |
| 215 | |
| 216 | • **columns**: `string` \| `string`[] \| `Record`<`string`, `string`> \| `Map`<`string`, `string`> |
| 217 | |
| 218 | #### Returns |
| 219 | |
| 220 | `this` |
| 221 | |
| 222 | #### Example |
| 223 | |
| 224 | ```ts |
| 225 | new Map([["combined", "a + b"], ["c", "c"]]) |
| 226 | |
| 227 | Columns will always be returned in the order given, even if that order is different than |
| 228 | the order used when adding the data. |
| 229 | |
| 230 | Note that you can pass in a `Record<string, string>` (e.g. an object literal). This method |
| 231 | uses `Object.entries` which should preserve the insertion order of the object. However, |
| 232 | object insertion order is easy to get wrong and `Map` is more foolproof. |
| 233 | ``` |
| 234 | |
| 235 | *** |
| 236 | |
| 237 | ### toArray() |
| 238 | |
| 239 | > **toArray**(`options`?): `Promise`<`any`[]> |
| 240 | |
| 241 | Collect the results as an array of objects. |
| 242 | |
| 243 | #### Parameters |
| 244 | |
| 245 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 246 | |
| 247 | #### Returns |
| 248 | |
| 249 | `Promise`<`any`[]> |
| 250 | |
| 251 | *** |
| 252 | |
| 253 | ### toArrow() |
| 254 | |
| 255 | > **toArrow**(`options`?): `Promise`<`Table`<`any`>> |
| 256 | |
| 257 | Collect the results as an Arrow |
| 258 | |
| 259 | #### Parameters |
| 260 | |
| 261 | • **options?**: `Partial`<`QueryExecutionOptions`> |
| 262 | |
| 263 | #### Returns |
| 264 | |
| 265 | `Promise`<`Table`<`any`>> |
| 266 | |
| 267 | #### See |
| 268 | |
| 269 | ArrowTable. |
| 270 | |
| 271 | *** |
| 272 | |
| 273 | ### where() |
| 274 | |
| 275 | > **where**(`predicate`): `this` |
| 276 | |
| 277 | A filter statement to be applied to this query. |
| 278 | |
| 279 | The filter should be supplied as an SQL query string. For example: |
| 280 | |
| 281 | #### Parameters |
| 282 | |
| 283 | • **predicate**: `string` |
| 284 | |
| 285 | #### Returns |
| 286 | |
| 287 | `this` |
| 288 | |
| 289 | #### Example |
| 290 | |
| 291 | ```ts |
| 292 | x > 10 |
| 293 | y > 0 AND y < 100 |
| 294 | x > 5 OR y = 'test' |
| 295 | |
| 296 | Filtering performance can often be improved by creating a scalar index |
| 297 | on the filter column(s). |
| 298 | ``` |