| 1 | # Filtering |
| 2 | |
| 3 | ## Pre and post-filtering |
| 4 | |
| 5 | LanceDB supports filtering of query results based on metadata fields. By default, post-filtering is |
| 6 | performed on the top-k results returned by the vector search. However, pre-filtering is also an |
| 7 | option that performs the filter prior to vector search. This can be useful to narrow down on |
| 8 | the search space on a very large dataset to reduce query latency. |
| 9 | |
| 10 | <!-- Setup Code |
| 11 | ```python |
| 12 | import lancedb |
| 13 | import numpy as np |
| 14 | uri = "data/sample-lancedb" |
| 15 | db = lancedb.connect(uri) |
| 16 | |
| 17 | data = [{"vector": row, "item": f"item {i}", "id": i} |
| 18 | for i, row in enumerate(np.random.random((10_000, 2)).astype('int'))] |
| 19 | |
| 20 | tbl = db.create_table("my_vectors", data=data) |
| 21 | ``` |
| 22 | --> |
| 23 | <!-- Setup Code |
| 24 | ```javascript |
| 25 | const vectordb = require('vectordb') |
| 26 | const db = await vectordb.connect('data/sample-lancedb') |
| 27 | |
| 28 | let data = [] |
| 29 | for (let i = 0; i < 10_000; i++) { |
| 30 | data.push({vector: Array(1536).fill(i), id: i, item: `item ${i}`, strId: `${i}`}) |
| 31 | } |
| 32 | const tbl = await db.createTable('myVectors', data) |
| 33 | ``` |
| 34 | --> |
| 35 | |
| 36 | === "Python" |
| 37 | |
| 38 | ```py |
| 39 | result = ( |
| 40 | tbl.search([0.5, 0.2]) |
| 41 | .where("id = 10", prefilter=True) |
| 42 | .limit(1) |
| 43 | .to_arrow() |
| 44 | ) |
| 45 | ``` |
| 46 | |
| 47 | === "TypeScript" |
| 48 | |
| 49 | === "@lancedb/lancedb" |
| 50 | |
| 51 | ```ts |
| 52 | --8<-- "nodejs/examples/filtering.ts:search" |
| 53 | ``` |
| 54 | |
| 55 | === "vectordb (deprecated)" |
| 56 | |
| 57 | ```ts |
| 58 | --8<-- "docs/src/sql_legacy.ts:search" |
| 59 | ``` |
| 60 | |
| 61 | ## SQL filters |
| 62 | |
| 63 | Because it's built on top of [DataFusion](https://github.com/apache/arrow-datafusion), LanceDB |
| 64 | embraces the utilization of standard SQL expressions as predicates for filtering operations. |
| 65 | It can be used during vector search, update, and deletion operations. |
| 66 | |
| 67 | Currently, Lance supports a growing list of SQL expressions. |
| 68 | |
| 69 | - `>`, `>=`, `<`, `<=`, `=` |
| 70 | - `AND`, `OR`, `NOT` |
| 71 | - `IS NULL`, `IS NOT NULL` |
| 72 | - `IS TRUE`, `IS NOT TRUE`, `IS FALSE`, `IS NOT FALSE` |
| 73 | - `IN` |
| 74 | - `LIKE`, `NOT LIKE` |
| 75 | - `CAST` |
| 76 | - `regexp_match(column, pattern)` |
| 77 | - [DataFusion Functions](https://arrow.apache.org/datafusion/user-guide/sql/scalar_functions.html) |
| 78 | |
| 79 | For example, the following filter string is acceptable: |
| 80 | |
| 81 | === "Python" |
| 82 | |
| 83 | ```python |
| 84 | tbl.search([100, 102]) \ |
| 85 | .where("(item IN ('item 0', 'item 2')) AND (id > 10)") \ |
| 86 | .to_arrow() |
| 87 | ``` |
| 88 | |
| 89 | === "TypeScript" |
| 90 | |
| 91 | === "@lancedb/lancedb" |
| 92 | |
| 93 | ```ts |
| 94 | --8<-- "nodejs/examples/filtering.ts:vec_search" |
| 95 | ``` |
| 96 | |
| 97 | === "vectordb (deprecated)" |
| 98 | |
| 99 | ```ts |
| 100 | --8<-- "docs/src/sql_legacy.ts:vec_search" |
| 101 | ``` |
| 102 | |
| 103 | If your column name contains special characters or is a [SQL Keyword](https://docs.rs/sqlparser/latest/sqlparser/keywords/index.html), |
| 104 | you can use backtick (`` ` ``) to escape it. For nested fields, each segment of the |
| 105 | path must be wrapped in backticks. |
| 106 | |
| 107 | === "SQL" |
| 108 | |
| 109 | ```sql |
| 110 | `CUBE` = 10 AND `column name with space` IS NOT NULL |
| 111 | AND `nested with space`.`inner with space` < 2 |
| 112 | ``` |
| 113 | |
| 114 | !!!warning "Field names containing periods (`.`) are not supported." |
| 115 | |
| 116 | Literals for dates, timestamps, and decimals can be written by writing the string |
| 117 | value after the type name. For example |
| 118 | |
| 119 | === "SQL" |
| 120 | |
| 121 | ```sql |
| 122 | date_col = date '2021-01-01' |
| 123 | and timestamp_col = timestamp '2021-01-01 00:00:00' |
| 124 | and decimal_col = decimal(8,3) '1.000' |
| 125 | ``` |
| 126 | |
| 127 | For timestamp columns, the precision can be specified as a number in the type |
| 128 | parameter. Microsecond precision (6) is the default. |
| 129 | |
| 130 | | SQL | Time unit | |
| 131 | | -------------- | ------------ | |
| 132 | | `timestamp(0)` | Seconds | |
| 133 | | `timestamp(3)` | Milliseconds | |
| 134 | | `timestamp(6)` | Microseconds | |
| 135 | | `timestamp(9)` | Nanoseconds | |
| 136 | |
| 137 | LanceDB internally stores data in [Apache Arrow](https://arrow.apache.org/) format. |
| 138 | The mapping from SQL types to Arrow types is: |
| 139 | |
| 140 | | SQL type | Arrow type | |
| 141 | | --------------------------------------------------------- | ------------------ | |
| 142 | | `boolean` | `Boolean` | |
| 143 | | `tinyint` / `tinyint unsigned` | `Int8` / `UInt8` | |
| 144 | | `smallint` / `smallint unsigned` | `Int16` / `UInt16` | |
| 145 | | `int` or `integer` / `int unsigned` or `integer unsigned` | `Int32` / `UInt32` | |
| 146 | | `bigint` / `bigint unsigned` | `Int64` / `UInt64` | |
| 147 | | `float` | `Float32` | |
| 148 | | `double` | `Float64` | |
| 149 | | `decimal(precision, scale)` | `Decimal128` | |
| 150 | | `date` | `Date32` | |
| 151 | | `timestamp` | `Timestamp` [^1] | |
| 152 | | `string` | `Utf8` | |
| 153 | | `binary` | `Binary` | |
| 154 | |
| 155 | [^1]: See precision mapping in previous table. |
| 156 | |
| 157 | ## Filtering without Vector Search |
| 158 | |
| 159 | You can also filter your data without search. |
| 160 | |
| 161 | === "Python" |
| 162 | |
| 163 | ```python |
| 164 | tbl.search().where("id = 10").limit(10).to_arrow() |
| 165 | ``` |
| 166 | |
| 167 | === "TypeScript" |
| 168 | |
| 169 | === "@lancedb/lancedb" |
| 170 | |
| 171 | ```ts |
| 172 | --8<-- "nodejs/examples/filtering.ts:sql_search" |
| 173 | ``` |
| 174 | |
| 175 | === "vectordb (deprecated)" |
| 176 | |
| 177 | ```ts |
| 178 | --8<---- "docs/src/sql_legacy.ts:sql_search" |
| 179 | ``` |
| 180 | |
| 181 | !!!warning "If your table is large, this could potentially return a very large amount of data. Please be sure to use a `limit` clause unless you're sure you want to return the whole result set." |