FuzzyObjectIndex
A persistent fuzzy search index for object collections with weighted keys.
Pre-computes key texts and stores them on the Rust side for fast repeated searches. Use this when searching the same collection multiple times.
The type parameters after T only capture the key names passed to the
constructor so they can be checked against T; annotate variables as
FuzzyObjectIndex<T>.
Example
Section titled “Example”const index = new FuzzyObjectIndex(users, { keys: [{ name: 'name', weight: 2.0 }, 'email'],});
const results = index.search('john');// results[0].item → { name: 'John Smith', ... }
index.add({ name: 'New User', email: 'new@example.com' });index.destroy(); // free Rust-side memoryType Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
- |
S extends string |
string |
C extends string |
string |
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new FuzzyObjectIndex<
T,S,C>(items,options):FuzzyObjectIndex<T,S,C>
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
items |
readonly T[] |
options |
CheckedOptions<T, S, C, unknown> |
Returns
Section titled “Returns”FuzzyObjectIndex<T, S, C>
Throws
Section titled “Throws”If items is not an array or options.keys is not a
non-empty array of key paths / key configs.
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get size():
number
Number of items in the index.
Returns
Section titled “Returns”number
Methods
Section titled “Methods”add(
item):void
Add a single item to the index.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
item |
T |
Returns
Section titled “Returns”void
addMany()
Section titled “addMany()”addMany(
items):void
Add multiple items at once. If reading a key of any item throws, no item is added.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
items |
readonly T[] |
Returns
Section titled “Returns”void
closest()
Section titled “closest()”closest(
query,options?):T|null
Find the closest matching object, or null if no match: the item of the
first result of
search(query, { maxResults: 1, minScore, scoreMode, matchMode }).
The second argument accepts a number (minScore shorthand) or a
KeyClosestOptions object, whose scoreMode defaults to
'weighted' and matchMode to 'perKey'.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
query |
string |
options? |
number | KeyClosestOptions | null |
Returns
Section titled “Returns”T | null
Example
Section titled “Example”index.closest('smith', 0.9); // minScore 0.9index.closest('smith', { minScore: 0.9, scoreMode: 'matched' });index.closest('john tokyo', { scoreMode: 'max', matchMode: 'crossKey' });Throws
Section titled “Throws”If options is neither a number nor an object.
Throws
Section titled “Throws”If scoreMode is not 'weighted', 'matched' or 'max',
matchMode is not 'perKey' or 'crossKey', or minScore is not a
number or is NaN (a TypeError in the browser build).
destroy()
Section titled “destroy()”destroy():
void
Remove all items and free their Rust-side memory.
The index stays usable: afterwards it behaves as an empty index, and
add() / addMany() work as usual.
Returns
Section titled “Returns”void
remove()
Section titled “remove()”remove(
index):boolean
Remove the item at the given position (swap-remove semantics: the last
item moves into the freed slot, so its index changes).
Returns false when index is out of range.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index |
number |
Returns
Section titled “Returns”boolean
Throws
Section titled “Throws”If index is not a number.
Throws
Section titled “Throws”If index is not an integer (e.g. NaN or 1.5).
search()
Section titled “search()”search(
query,options?):ObjectSearchResult<T>[]
Search for objects matching the query.
The second argument accepts a number (maxResults shorthand) or an options
object, like FuzzyIndex.search().
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
query |
string |
options? |
number | ObjectIndexSearchOptions | null |
Returns
Section titled “Returns”ObjectSearchResult<T>[]
Throws
Section titled “Throws”If options is neither a number nor an object.
serialize()
Section titled “serialize()”serialize():
Uint8Array
Serialize the index and its items to a Buffer (NodeBuffer: a Uint8Array
in TypeScript programs without the Node.js types).
Items must be JSON-serializable. Pass the result to
FuzzyObjectIndex.deserialize() to reconstruct the index.
Returns
Section titled “Returns”Uint8Array
deserialize()
Section titled “deserialize()”
staticdeserialize<T>(data):FuzzyObjectIndex<T>
Reconstruct a FuzzyObjectIndex from data produced by serialize():
a Buffer, any other ArrayBufferView (e.g. Uint8Array) or an ArrayBuffer.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
unknown |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
data |
ArrayBufferLike | ArrayBufferView<ArrayBufferLike> |
Returns
Section titled “Returns”FuzzyObjectIndex<T>
Throws
Section titled “Throws”If data is not binary data.
Throws
Section titled “Throws”If data is malformed (the message starts with
"Invalid FuzzyObjectIndex data").