The Rust core (lancedb::index::scalar::NGramIndexBuilder) and the Python
SDK already support the NGram scalar index, and the Node.js SDK can
create one on Embedded with `Index.plugin("ngram")`. This change adds a
dedicated `Index.ngram()` factory. NGram is a scalar index on a UTF-8
string column that accelerates substring filters such as contains(col,
'needle'), along with some LIKE and regular-expression predicates, by
narrowing a scan to rows that share the query's trigrams.
Add an ngram() napi factory and Index.ngram() in the TypeScript SDK,
mirroring the existing btree and fm scalar index factories, with tests
for both factories. On Embedded, the query plan uses the index and the
exact substring filter excludes rows that only share the query’s
trigrams. On Enterprise, `Index.ngram()` sends `index_type: "NGRAM"`,
while `Index.plugin("ngram")` sends `index_type: "PLUGIN"` with
`plugin_type: "NGRAM"`.
---------
Co-authored-by: Xuanwo <github@xuanwo.io>
10 KiB
@lancedb/lancedb • Docs
@lancedb/lancedb / Index
Class: Index
Methods
bitmap()
static bitmap(): Index
Create a bitmap index.
A Bitmap index stores a bitmap for each distinct value in the column for every row.
This index works best for low-cardinality columns, where the number of unique values is small (i.e., less than a few hundreds).
Returns
bloomFilter()
static bloomFilter(options?): Index
Create a Bloom filter index.
Bloom filters accelerate equality and membership filters by skipping groups of rows that cannot match. Candidate rows are checked to remove false positives. This index uses less space than a btree or bitmap index, but does not accelerate range filters. Unset options use Lance's defaults.
Parameters
- options?:
BloomFilterOptions
Returns
Example
Tuning a Bloom filter on an Embedded table:
await table.createIndex("id", {
config: Index.bloomFilter({ numberOfItems: 4096, probability: 0.01 }),
});
btree()
static btree(): Index
Create a btree index
A btree index is an index on a scalar columns. The index stores a copy of the column in sorted order. A header entry is created for each block of rows (currently the block size is fixed at 4096). These header entries are stored in a separate cacheable structure (a btree). To search for data the header is used to determine which blocks need to be read from disk.
For example, a btree index in a table with 1Bi rows requires sizeof(Scalar) * 256Ki bytes of memory and will generally need to read sizeof(Scalar) * 4096 bytes to find the correct row ids.
This index is good for scalar columns with mostly distinct values and does best when the query is highly selective.
The btree index does not currently have any parameters though parameters such as the block size may be added in the future.
Returns
fm()
static fm(): Index
Create an FM-Index.
An FM-Index is a scalar index on string or binary columns that accelerates
substring search, i.e. contains(col, 'needle'). Unlike the tokenized
full-text-search index, it matches arbitrary substrings of the raw bytes.
Returns
fts()
static fts(options?): Index
Create a full text search index
A full text search index is an index on a string column, so that you can conduct full text searches on the column.
The results of a full text search are ordered by relevance measured by BM25.
You can combine filters with full text search.
Parameters
- options?:
Partial<FtsOptions>
Returns
hnswPq()
static hnswPq(options?): Index
Create a hnswPq index
HNSW-PQ stands for Hierarchical Navigable Small World - Product Quantization. It is a variant of the HNSW algorithm that uses product quantization to compress the vectors.
Parameters
- options?:
Partial<HnswPqOptions>
Returns
hnswSq()
static hnswSq(options?): Index
Create a hnswSq index
HNSW-SQ stands for Hierarchical Navigable Small World - Scalar Quantization. It is a variant of the HNSW algorithm that uses scalar quantization to compress the vectors.
Parameters
- options?:
Partial<HnswSqOptions>
Returns
ivfFlat()
static ivfFlat(options?): Index
Create an IvfFlat index
This index groups vectors into partitions of similar vectors. Each partition keeps track of a centroid which is the average value of all vectors in the group.
During a query the centroids are compared with the query vector to find the closest partitions. The vectors in these partitions are then searched to find the closest vectors.
The partitioning process is called IVF and the num_partitions parameter controls how
many groups to create.
Note that training an IVF FLAT index on a large dataset is a slow operation and currently is also a memory intensive operation.
Parameters
- options?:
Partial<IvfFlatOptions>
Returns
ivfPq()
static ivfPq(options?): Index
Create an IvfPq index
This index stores a compressed (quantized) copy of every vector. These vectors are grouped into partitions of similar vectors. Each partition keeps track of a centroid which is the average value of all vectors in the group.
During a query the centroids are compared with the query vector to find the closest partitions. The compressed vectors in these partitions are then searched to find the closest vectors.
The compression scheme is called product quantization. Each vector is divided into
subvectors and then each subvector is quantized into a small number of bits. the
parameters num_bits and num_subvectors control this process, providing a tradeoff
between index size (and thus search speed) and index accuracy.
The partitioning process is called IVF and the num_partitions parameter controls how
many groups to create.
Note that training an IVF PQ index on a large dataset is a slow operation and currently is also a memory intensive operation.
Parameters
- options?:
Partial<IvfPqOptions>
Returns
ivfRq()
static ivfRq(options?): Index
Create an IvfRq index
IVF-RQ (RabitQ Quantization) compresses vectors using RabitQ quantization and organizes them into IVF partitions.
The compression scheme is called RabitQ quantization. Each dimension is quantized into a small number of bits.
The parameters num_bits and num_partitions control this process, providing a tradeoff
between index size (and thus search speed) and index accuracy.
The partitioning process is called IVF and the num_partitions parameter controls how
many groups to create.
Note that training an IVF RQ index on a large dataset is a slow operation and currently is also a memory intensive operation.
Parameters
- options?:
Partial<IvfRqOptions>
Returns
labelList()
static labelList(): Index
Create a label list index.
LabelList index is a scalar index that can be used on List<T> columns to
support queries with array_has_all and array_has_any
using an underlying bitmap index.
Returns
ngram()
static ngram(): Index
Create an NGram index.
An NGram index is a scalar index on a UTF-8 string column that accelerates
substring filters such as contains(col, 'needle'), along with some LIKE
and regular-expression predicates. It tokenizes values into overlapping
character trigrams and narrows a scan to the rows that share the query's
trigrams. The exact predicate is then evaluated on those candidates.
Queries shorter than three characters fall back to a scan.
Returns
Example
await table.createIndex("text", { config: Index.ngram() });
const rows = await table.query().where("contains(text, 'needle')").toArray();
plugin()
static plugin(indexType, params?): Index
Create a scalar index of a type recognized by a registered database plugin.
Use this for index types without a dedicated factory on Index.
Prefer a dedicated factory when one is available.
An unknown plugin name or invalid parameters cause createIndex to fail.
Parameters
-
indexType:
stringThe index type, case-insensitive. Surrounding whitespace is ignored. Must not be empty. -
params?:
string|Record<string,unknown> The index parameters, as an object or as a string holding a JSON object. They are passed as given. Each plugin defines its parameters and defaults; refer to the plugin's documentation for guidance.
Returns
Throws
If indexType is empty, or params is not a JSON object.
Example
await table.createIndex("id", {
config: Index.plugin("bloomfilter", { number_of_items: 4096 }),
});
rtree()
static rtree(options?): Index
Create an RTree index.
An RTree index is a scalar index on a GeoArrow geometry column, that is, a
column whose Arrow field metadata sets ARROW:extension:name to a GeoArrow
type such as geoarrow.point, geoarrow.polygon or geoarrow.wkb. It
stores the bounding box of each geometry and accelerates spatial filters.
The index accelerates ST_Intersects, ST_Contains, ST_Within,
ST_Touches, ST_Crosses, ST_Overlaps, ST_Covers and ST_CoveredBy
between the indexed column and a constant geometry, in either argument
order, for example
ST_Within(geom, ST_GeomFromText('POLYGON ((...))')), as well as
geom IS NULL. These filters can be combined with other conditions using
AND.
See the R-tree section of https://docs.lancedb.com/indexing/scalar-index for storing geometry, the other spatial functions, and spatial filters on vector search.
Remote tables send the RTREE index type to the server, which must
support it.
Parameters
- options?:
Partial<RTreeOptions>
Returns
Example
await table.createIndex("geom", { config: Index.rtree() });
await table
.query()
.where("ST_Intersects(geom, ST_GeomFromText('POLYGON ((0 0, 4 0, 0 4, 0 0))'))")
.toArray();
zoneMap()
static zoneMap(): Index
Create a ZoneMap index.
This index stores min/max summaries for ranges of rows and skips ranges that cannot match a filter. It works best for numeric or temporal columns whose values are correlated with row order, such as timestamps or sequential IDs.
For strings, binary data, and supported types wider than eight bytes, this index uses per-fragment write seeds. By default, these use version 0 and do not activate the version 1 table capability.
A commit that explicitly sets the table configuration key
lance.zone_write_seeds.version to "1" activates a persistent reader and
writer capability. Upgrade all readers and writers before opting in: clients
that do not support this capability cannot access the resulting snapshots.
Dropping the index or restoring an older version does not remove the
capability from subsequent snapshots.
Returns
Example
await table.createIndex("timestamp", { config: Index.zoneMap() });