Usage

Create a new ZBSearch instance

Create a new ZBSearch instance to store and search documents.

We can create a new instance (from now on database) with an indexing schema.
The schema represents the searchable properties of the document to be inserted. Not all properties need to be indexed, but only those that we want to be able to search for.

If you want to learn more and see real-world examples, check out this blog post we wrote about schema optimization.

Schema properties and types

The schema is an object where the keys are the property names and the values are the property types.
ZBSearch supports the following types:

TypeDescriptionExample
stringA string of characters.'Hello world'
numberA numeric value, either float or integer.42
booleanA boolean value.true
enumAn enum value.'drama'
geopointA geopoint value.{ lat: 40.7128, lon: 74.0060 }
string[]An array of strings.['red', 'green', 'blue']
number[]An array of numbers.[42, 91, 28.5]
boolean[]An array of booleans.[true, false, false]
enum[]An array of enums.['comedy', 'action', 'romance']
vector[<size>]A vector of numbers to perform vector search on.[0.403, 0.192, 0.830]

A database can be as simple as:

import { create } from "zbsearch";

const db = create({
  schema: {
    word: "string",
  },
});

or more variegated:

import { create } from "zbsearch";

const movieDB = create({
  schema: {
    title: "string",
    director: "string",
    plot: "string",
    year: "number",
    isFavorite: "boolean",
  },
});

Schema-less usage (schema inference)

The schema is optional. If you omit it, ZBSearch infers the type of every document property the first time it sees it, and indexes it on the fly:

import { create, insert, search } from "zbsearch";

const db = create();

await insert(db, { title: "The Godfather", year: 1972, cast: { director: "Coppola" } });

// db.schema is now: { title: "string", year: "number", cast: { director: "string" } }
await search(db, { term: "godfather" });

Inferred types follow these rules:

Document valueInferred type
stringstring
numbernumber
booleanboolean
string[] / number[] / boolean[]string[] / number[] / boolean[]
{ lat: number, lon: number }geopoint
nested objectrecursively inferred as nested properties
empty array, null, undefineddeferred until a concrete value appears

A few things worth knowing:

  • Types lock on first sight. If year is first seen as a number, a later document with year: "1972" is rejected with a SCHEMA_VALIDATION_FAILURE error, exactly as with a declared schema.
  • Vectors are never inferred, because a number[] is indistinguishable from an embedding. Declare vector (embedding) properties explicitly and let everything else be inferred:
const db = create({
  schema: { embedding: "vector[384]" },
  inferSchema: true, // required: a provided schema is strict by default
});
  • Providing a schema keeps the strict behavior: properties not declared in the schema are stored but not indexed, unless you opt into inference with inferSchema: true as above. Conversely, create({ inferSchema: false }) stores documents without indexing anything.
  • Inferred schemas are preserved by save/load, so schema-less databases survive persistence round-trips.

Nested properties

ZBSearch supports nested properties natively. Just add them as you would typically do in any JavaScript object:

const movieDB = create({
  schema: {
    title: "string",
    plot: "string",
    cast: {
      director: "string",
      leading: "string",
      supporting: "string[]",
    },
    year: "number",
    isFavorite: "boolean",
  },
});

insert(movieDB, {
  title: "The Godfather",
  plot: "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.",
  cast: {
    director: "Francis Ford Coppola",
    leading: "Marlon Brando",
    supporting: ["Al Pacino", "James Caan", "Robert Duvall"],
  },
  year: 1972,
  isFavorite: true,
});

Vector properties

Since version 1.2.0, ZBSearch supports vector search.
To run vector queries, you first need to initialize a vector property in the schema:

const db = create({
  schema: {
    title: "string",
    embedding: "vector[10]", // replace 10 with the appropriate size of your vector
  },
});

insert(db, {
  title: "The Godfather",
  embedding: [
    -0.8469661901208547, 0.6762289692745016, 0.3294302068627739,
    -0.9269241187762711, -0.8340635986042049, -0.9940330715457502,
    -0.46761552816396046, 0.2818135926099674, -0.5812061227183709,
    0.6443446315273054,
  ],
});

Please note that the size of the vector must be specified in the schema.
The size of the vector is the number of elements that the vector contains, so make sure to specify the correct size, as performing search on vectors of different sizes will result in unpredictable and mostly wrong results. For performance reasons, we recommend using one vector property per database, even though it's possible to have multiple vector properties in the same ZBSearch instance.

If you're using vector properties to search through embeddings, we highly recommend using HuggingFace's gte-small model, which has a vector size of 384.

There is a great article written by Supabase explaining why it might be a better option than OpenAI's text-embedding-ada-002 model: https://supabase.com/blog/fewer-dimensions-are-better-pgvector.

Choosing an IVF index

By default, ZBSearch uses a flat vector index: every query compares your search vector against all stored embeddings. That is simple, exact, and ideal for small or medium datasets (roughly up to a few thousand vectors), prototypes, and cases where recall must be perfect.

For larger collections, switch to an IVF (Inverted File) index. IVF partitions vectors into clusters at insert time and only searches a subset of clusters at query time, trading a small amount of recall for much faster searches. Enable it when vector search latency grows with your dataset size and you can tolerate approximate nearest-neighbor results.

import { create } from "zbsearch";
import { ivf } from "zbsearch/trees/vector-ivf";

const db = create({
  schema: {
    title: "string",
    embedding: "vector[384]",
  },
  indexes: {
    embedding: ivf({
      nlist: 64,    // number of clusters (default: 64)
      nprobe: 16,   // clusters searched per query (default: nlist / 4, capped at 32)
      trainMin: 32, // minimum vectors before training centroids (default: min(nlist, 32))
    }),
  },
});

Pros: significantly faster vector search on large indexes, lower per-query CPU cost, and configurable speed/recall via nprobe (higher nprobe = more accurate but slower).

Cons: results are approximate-you may miss the true nearest neighbor-centroids must be trained once enough vectors are inserted (trainMin), tuning nlist and nprobe takes experimentation, and persistence requires passing the same indexes config when loading a saved database. Start with flat search; reach for IVF when profiling shows vector queries are a bottleneck.

The centroids are trained once, on the first trainMin vectors. The default (min(nlist, 32)) is enough for tests but gives poor clusters on real data: train on at least 20 × nlist vectors. IVF only helps when the embeddings are clustered, as real text embeddings are; on uniformly random vectors it cannot find the nearest neighbours.

Vector quantization

By default every vector is stored as float32: 1,536 bytes for a 384-dimensional embedding, and about 20 characters per number once saved as JSON. Both flat() and ivf() accept a quantization option that stores compact codes instead:

quantizationBytes per 384-d vectorHow it scores
'none' (default)1,536Exact float32 dot product.
'int8'392Per-vector scale and offset, one signed byte per dimension, dot product against the float32 query.
'binary'48One sign bit per dimension, Hamming distance. Coarse on its own: use it with rescore.
a quantizer objectdependsFor example product quantization from @zbsearch/plugin-vector-quantizer.
import { create } from "zbsearch";
import { flat } from "zbsearch/trees/vector-flat";
import { ivf } from "zbsearch/trees/vector-ivf";

const db = create({
  schema: {
    title: "string",
    embedding: "vector[384]",
  },
  indexes: {
    // About 4x less memory, near-exact results.
    embedding: flat({ quantization: "int8" }),

    // Hamming-distance candidates, rescored against int8 copies: fastest scan.
    // embedding: flat({ quantization: "binary", rescore: "int8", oversample: 10 }),

    // Quantization works the same way with IVF.
    // embedding: ivf({ nlist: 256, nprobe: 16, trainMin: 8192, quantization: "int8" }),
  },
});
  • rescore: 'int8' | 'float32' keeps a second, more precise copy of each vector. A search ranks every vector with the compact codes, keeps the best limit × oversample candidates (oversample defaults to 10) and rescores them with the precise copy. When the search has a limit, count is then at most (offset + limit) × oversample.
  • Similarity thresholds (similarity) apply to the quantized scores. int8 scores are very close to the exact cosine; binary scores are the angle estimate cos(π · hamming / dimensions).
  • Quantized indexes store their vectors in one contiguous buffer, so they also avoid the per-vector object overhead of the default index.
  • save() writes the codes as base64 typed arrays instead of arrays of JSON numbers, and the index exposes toBinarySnapshot() (typed arrays plus a small JSON meta object) for binary persistence formats. int8 and binary indexes load without any indexes configuration; custom quantizers need the same configuration at load time.
  • With includeVectors: true, vectors are reconstructed from the codes (or from the rescore copy when there is one), so they are approximate.

Measured with benchmarks/vector-quantization.js on 20,000 synthetic clustered 384-dimensional vectors (200 queries, recall@10 against exact float32 search, Node.js 22, one core). Numbers on a real embedding set are still to be published; the benchmark's header explains how to produce them for BEIR SciFact.

IndexRecall@10Vector memoryJSON sizeMedian query
flat, none1.00035 MB154 MB7.6 ms
flat, int80.9838.6 MB10.1 MB9.4 ms
flat, binary0.3101.9 MB1.3 MB1.2 ms
flat, binary + int8 rescore0.8989.5 MB11.3 MB1.2 ms
flat, PQ (m = 48) + int80.95810 MB11.9 MB2.0 ms
IVF (nlist 141, nprobe 16)0.99535 MB155 MB4.0 ms
IVF, int80.9799.1 MB10.4 MB3.5 ms
IVF, binary + int8 rescore0.89110 MB11.7 MB0.6 ms

int8 is about memory and disk size, not speed: JavaScript has no SIMD, so an int8 dot product costs about as much as a float32 one. An int8 index of 160,000 × 384 vectors holds 63.4 MB (60.4 MiB) of codes and ids, about 70 MB including the id map (measured with --memory-only=160000). The code buffer grows in 12.5% steps; call db.data.index.vectorIndexes.embedding.node.shrinkToFit() after a bulk load to release the spare capacity.

Indexed but not stored properties

By default, ZBSearch keeps every inserted document in its documents store and returns it in search results. When the original text already lives elsewhere (a file, a database, a cache) and you only need IDs and a few metadata fields back, use the store option to index properties without storing them:

const db = create({
  schema: {
    title: "string",
    text: "string",
    year: "number",
    embedding: "vector[384]",
  },
  store: {
    // Indexed and searchable, but not kept in the documents store.
    // Dot-separated paths exclude nested properties; a path also excludes everything nested under it.
    exclude: ["text"],
  },
});

insert(db, { id: "p1", title: "Introduction", text: "A long passage...", year: 2020, embedding: [/* ... */] });

getByID(db, "p1"); // { id: "p1", title: "Introduction", year: 2020 }

Excluded properties are indexed exactly as before: full-text search, filters (where), exact, boosting and BM25 scoring all work on them. The inserted document is never modified: the documents store keeps a copy without the excluded properties. What changes:

  • Search results and getByID return the stored properties only.
  • facets, groupBy and distinctOn read the stored documents, so using them on an excluded property throws a PROPERTY_NOT_STORED error (also for an excluded property outside the schema with distinctOn). distinctOn on a vector property that is not stored throws VECTOR_NOT_STORED.
  • Excluded string properties are not sortable: the sorter keeps its own copy of every sortable value, which would keep the text in memory. Excluded numbers and booleans stay sortable.
  • exact: true cannot check the case of an excluded property's text: for those properties it keeps the documents the index matched exactly (case-insensitively).
  • Documents that are instances of a class with getters or methods (for example, getters that read #private fields) are stored as a view of the instance that hides the unstored properties, so that the class code keeps working. The view keeps the instance in memory, with all its data: for such documents, exclude (and leaving vectors out of the store) hides the properties but does not save memory. Plain objects, and instances of classes without getters or methods, are copied without the unstored properties. A toJSON method of the class runs on the instance, and its result is filtered by key, so that JSON.stringify, search hits and snapshots serialize what the view shows: keys the view hides (unstored properties) are dropped, and keys it changes (vectors set to null or rebuilt on search hits) take the view's value. Unstored values that toJSON returns under other keys are not filtered.

Vectors are not stored by default

Vector properties are not kept in the documents store unless you opt in: the vector index already holds every vector. With includeVectors: true, search results rebuild them from the vector index, which holds a normalized (unit-length) Float32 copy, so the returned vectors are normalized and may differ slightly from the inserted ones. To keep the original vectors in the documents (as ZBSearch did up to 4.0.x), set store.vectors:

const db = create({
  schema: { title: "string", embedding: "vector[384]" },
  store: { vectors: true },
});

Set it if you rebuild indexes from the stored documents (for example, by reading getByID or db.data.docs), or if you need the exact inserted values back.

Removing and updating documents

remove, update and upsert keep working without the stored values. The index keeps what it needs to remove a document:

Property typeWhat is kept for removal
stringNothing: the per-document term frequencies the index already keeps for BM25 list the terms to remove.
string[]The distinct terms of each document, as 32-bit IDs into a per-property term dictionary.
number, number[], enum, enum[], geopointThe value itself (it is small).
boolean, boolean[], vectorsNothing: they are removed by document ID.

So excluding text costs nothing to keep removal correct, and removing a document whose text is excluded is faster, as it does not tokenize the text again. Excluding small scalar properties saves little memory, since their values are kept for removal anyway: exclude them only to keep them out of the results.

Highlighting excluded text

@zbsearch/highlight takes the text to highlight as an argument, so pass your own copy: new Highlight().highlight(await getMyText(hit.id), term). @zbsearch/plugin-match-highlight records the positions of excluded properties at insert time, and searchWithHighlight accepts a getSourceText(id, property) callback to compute them at search time instead:

const results = await searchWithHighlight(db, { term: "passage" }, undefined, {
  getSourceText: (id, property) => myTextStore.get(id),
});

Saving and loading

save stores the store configuration and the removal data with the rest of the database, and load restores them, so documents inserted after loading are stored the same way. Snapshots saved by 4.0.x carry no configuration: the one given to create applies to new documents, while the documents already in the snapshot (vectors included) are kept and returned as they were.

Memory and snapshot size

On a synthetic corpus of 10,000 passages (150-250 words from a Zipf-distributed 40k-word vocabulary, seven metadata fields and a vector[384] embedding), measured with benchmarks/store-exclude.js on Node 22:

ConfigurationHeapJSON snapshot
Everything stored (store: { vectors: true }, the 4.0.x behavior)175 MB206 MB
Default (vectors not stored)150 MB (-14%)130 MB (-37%)
store: { exclude: ['text'] }138 MB (-22%)105 MB (-49%)

Without the embedding, excluding text takes the heap from 143 MB to 134 MB (-6%) and the snapshot from 53 MB to 29 MB (-46%): the snapshot saved the text twice, in the documents and in the sorter. Most of the remaining heap is the inverted index itself (the radix tree and the per-document term frequencies used for scoring), which excluding properties from the store does not change. To shrink that part, freeze the index.

Instance ID

Every ZBSearch instance has a unique id property, which can be used to identify a given instance when working with multiple databases.

You can customize it by passing an id property during the creation of the instance:

import { create } from "zbsearch";

const db = create({
  schema: {
    word: "string",
  },
  id: "my-orama-instance",
});

On this page