feat(nodejs): expose the NGram scalar index (#4908)

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>
This commit is contained in:
BHAWESHBHASKAR
2026-12-01 21:39:01 +08:00
committed by GitHub
co-authored by Xuanwo
parent 144c07cee3
commit fb853bae75
5 changed files with 92 additions and 2 deletions
+27
View File
@@ -1303,6 +1303,33 @@ describe("When creating an index", () => {
expect(indices2.length).toBe(0);
});
it.each([
["Index.ngram()", () => Index.ngram()],
['Index.plugin("ngram")', () => Index.plugin("ngram")],
])("uses an NGram index for substring search via %s", async (_, config) => {
const db = await connect(tmpDir.name);
const ngramTable = await db.createTable("ngram_table", [
{ id: 0, text: "abcd" },
// Contains both trigrams but fails the exact substring predicate.
{ id: 1, text: "abcXbcd" },
{ id: 2, text: "prefix abcd suffix" },
{ id: 3, text: "unrelated" },
]);
await ngramTable.createIndex("text", { config: config() });
const indices = await ngramTable.listIndices();
expect(indices).toEqual([
expect.objectContaining({ indexType: "NGram", columns: ["text"] }),
]);
const stats = await ngramTable.indexStats(indices[0].name);
expect(stats?.numIndexedRows).toBe(4);
const query = ngramTable.query().where("contains(text, 'abcd')");
expect(await query.explainPlan(true)).toContain("ScalarIndexQuery");
const rows = await query.toArray();
expect(rows.map((row) => row.id).sort((a, b) => a - b)).toEqual([0, 2]);
});
it("should preserve canonical nested field paths across index lifecycle", async () => {
const db = await connect(tmpDir.name);
const nestedSchema = new Schema([