Skip to content

KeyedFuzzyIndex

A persistent multi-key fuzzy search index backed by Rust-side data.

Holds key text arrays and weights in memory on the Rust side, avoiding repeated FFI overhead for applications that search the same dataset multiple times with multiple keys. Pre-computes the search representation of every key text, eliminating per-search string conversion overhead. Memory is freed when the JavaScript garbage collector collects the instance or when destroy() is called explicitly.

Typically wrapped by a JS-side FuzzyObjectIndex class that maps results back to original objects.

new KeyedFuzzyIndex(keyTexts, weights): KeyedFuzzyIndex

Create a new KeyedFuzzyIndex.

keyTexts[k] is an array of strings for key k, one per item. All inner arrays must have the same length (the number of items).

Parameter Type
keyTexts readonly (readonly string[])[]
weights readonly number[]

KeyedFuzzyIndex

get size(): number

Return the number of items in the index.

number

add(keyValues): void

Add a single item to the index.

keyValues must have the same length as the number of keys. Throws if the length does not match.

Parameter Type
keyValues readonly string[]

void


addMany(itemsKeyValues): void

Add multiple items to the index at once.

Each element of itemsKeyValues is an array of key values for one item. Throws if any element has the wrong number of key values; every element is checked first, so on error no item is added.

Parameter Type
itemsKeyValues readonly (readonly string[])[]

void


closest(query, options?): number | null

Find the index of the closest matching item.

Returns the index of the best match, or null if no match is found: the index of the first result of search(query, { maxResults: 1, minScore, scoreMode, matchMode }).

The second argument accepts either a number (minScore shorthand) or a KeyClosestOptions object: minScore makes it return null when the best match scores below the threshold, and scoreMode and matchMode work like the search() options of the same names (defaults 'weighted' and 'perKey').

Use the returned index to look up the item in your own data array.

Parameter Type
query string
options? number | KeyClosestOptions | null

number | null


destroy(): void

Free the internal data. After calling this, the index is empty.

The key configuration is kept, so the index stays usable: it behaves as an empty index and add() / addMany() work as before.

void


remove(index): boolean

Remove the item at the given index.

Uses swap-remove for O(1) performance: the last item moves into the freed slot. Returns false, removing nothing, if index is out of range (negative, or not less than size). Throws a TypeError if index is not a number and a RangeError if it is not an integer (NaN, ±Infinity or a fraction), like FuzzyObjectIndex.remove().

Parameter Type
index number

boolean


search(query, options?): KeySearchResult[]

Search the index for items matching the query.

Returns results sorted by combined score (best match first), exactly like searchKeys() on the same key texts and weights.

The second argument accepts either a number (maxResults shorthand) or a KeySearchOptions object, whose matchMode selects how the query is matched against the keys and scoreMode how the per-key scores are combined. maxResults must be a non-negative integer or Infinity.

Parameter Type
query string
options? number | KeySearchOptions | null

KeySearchResult[]


serialize(): Uint8Array

Serialize the index to a compact binary format.

The returned Buffer can be written to disk, stored in IndexedDB, or transferred over the network. Use KeyedFuzzyIndex.deserialize() to reconstruct the index.

Uint8Array


static deserialize(data): KeyedFuzzyIndex

Reconstruct a KeyedFuzzyIndex from a previously serialized Buffer, or any other Uint8Array holding the same bytes (such as the output of the browser build’s serialize()).

Parameter Type
data Uint8Array<ArrayBufferLike> | Uint8Array<ArrayBufferLike>

KeyedFuzzyIndex