Skip to content

Commit

Permalink
feat(nodejs): add bitmap and label list index types in nodejs (lanced…
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyxu authored Aug 11, 2024
1 parent b2317c9 commit 694ca30
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
20 changes: 20 additions & 0 deletions nodejs/__test__/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import {
Float64,
Int32,
Int64,
List,
Schema,
Utf8,
makeArrowTable,
} from "../lancedb/arrow";
import {
Expand Down Expand Up @@ -331,6 +333,7 @@ describe("When creating an index", () => {
const schema = new Schema([
new Field("id", new Int32(), true),
new Field("vec", new FixedSizeList(32, new Field("item", new Float32()))),
new Field("tags", new List(new Field("item", new Utf8(), true))),
]);
let tbl: Table;
let queryVec: number[];
Expand All @@ -346,6 +349,7 @@ describe("When creating an index", () => {
vec: Array(32)
.fill(1)
.map(() => Math.random()),
tags: ["tag1", "tag2", "tag3"],
})),
{
schema,
Expand Down Expand Up @@ -428,6 +432,22 @@ describe("When creating an index", () => {
}
});

test("create a bitmap index", async () => {
await tbl.createIndex("id", {
config: Index.bitmap(),
});
const indexDir = path.join(tmpDir.name, "test.lance", "_indices");
expect(fs.readdirSync(indexDir)).toHaveLength(1);
});

test("create a label list index", async () => {
await tbl.createIndex("tags", {
config: Index.labelList(),
});
const indexDir = path.join(tmpDir.name, "test.lance", "_indices");
expect(fs.readdirSync(indexDir)).toHaveLength(1);
});

test("should be able to get index stats", async () => {
await tbl.createIndex("id");

Expand Down
23 changes: 23 additions & 0 deletions nodejs/lancedb/indices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,29 @@ export class Index {
return new Index(LanceDbIndex.btree());
}

/**
* 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).
*/
static bitmap() {
return new Index(LanceDbIndex.bitmap());
}

/**
* Create a label list index.
*
* LabelList index is a scalar index that can be used on `List<T>` columns to
* support queries with `array_contains_all` and `array_contains_any`
* using an underlying bitmap index.
*/
static labelList() {
return new Index(LanceDbIndex.labelList());
}

/**
* Create a full text search index
*
Expand Down
2 changes: 1 addition & 1 deletion nodejs/lancedb/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class QueryBase<NativeQueryType extends NativeQuery | NativeVectorQuery>
query: string,
options?: Partial<FullTextSearchOptions>,
): this {
let columns = null;
let columns: string[] | null = null;
if (options) {
if (typeof options.columns === "string") {
columns = [options.columns];
Expand Down
14 changes: 14 additions & 0 deletions nodejs/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ impl Index {
}
}

#[napi(factory)]
pub fn bitmap() -> Self {
Self {
inner: Mutex::new(Some(LanceDbIndex::Bitmap(Default::default()))),
}
}

#[napi(factory)]
pub fn label_list() -> Self {
Self {
inner: Mutex::new(Some(LanceDbIndex::LabelList(Default::default()))),
}
}

#[napi(factory)]
pub fn fts() -> Self {
Self {
Expand Down

0 comments on commit 694ca30

Please sign in to comment.