JavaScript · Retrieval and Streaming

Retrieval and Streaming

import * as usearch from 'usearch';import { DenseVectors } from 'usearch';import { DenseCursorLinks } from 'usearch';import { DenseDraws } from 'usearch';import { DenseCursorBatch } from 'usearch';import { DenseCursor } from 'usearch';import { DenseSearchStream } from 'usearch'; 3.0.0-rc.1 · 4c3e5023

get exports the vectors stored under one key into the arena, as f32 unless scalar names another kind:

const stored = index.get(42n, arena); // The vectors under a key, as f32 DenseVectors
using bitsArena = new usearch.Arena();
const packed = index.get(42n, bitsArena, { scalar: usearch.Scalar.U1x8 }); // Or as packed bits, in another arena

get shapes its rows from the width and stride the call reports: stored.data holds stored.rows rows of stored.columns values, stored.stride bytes apart, and no rows when the key is absent.

A cursor walks the index instead; each shard’s watermark is read as the walk enters it, so an entry once passed is never revisited. Without start keys it yields every entry in storage order; with them, the starts’ own entries first, then the entries those link to breadth-first, each once. It lands each batch of up to wanted keys in the arena, and beside them the keys each links to when links asks; the vectors themselves come from get:

using cursor = index.on(executor).cursor();
for (let batch = cursor.next(1024, arena, { links: true }); batch.entries; batch = cursor.next(1024, arena, { links: true })) {
  writeOut(batch.keys, batch.links.get(0), batch.entries);
}

Cursors and streams keep their walk state rather than a thread: every pull is an ordinary call on the executor of the receiver that opened them, and each keeps its index alive until freed. No residency implements the stream yet, so the engine answers Status.Unsupported, and a stream reports no telemetry, since the ABI gives a resumable walk none.

§ Sampling

sample draws keys around focus, or across the whole index, each hanging from a shown or earlier draw and placed in three dimensions beside the shown keys already on screen, so a view grows one connected sample as it zooms. Equal seeds, shown keys and focus draw equally, and a larger wanted draws a superset. The shown keys, their coordinates and the focus are read in place, so a deferred sample holds those arrays until it is final.

const drawn = index.on(executor).sample(256, arena, { shown, shownCoordinates: places, focus: near, seed: 42 });
drawn.keys; // sixteen bytes per draw
drawn.entryOrdinals; // which of its key's entries each draw is
drawn.parents.get(0); // the shown or earlier draw it hangs from, empty for a root
drawn.coordinates.data; // three f32 values per draw

API

Types

DenseSampleConfigOptional per-call settings of a sample; absent ones draw across the whole index with nothing shown.
usearch.ts:116 ↗
interface DenseSampleConfig

Fields

shown?KeysLikeThe keys already shown: never drawn again, and the paths reach toward them; read in place, never copied.
shownCoordinates?Float32ArrayWhere each shown key sits, three values apiece, end to end; read in place.
focus?KeysLikeThe keys to look around; absent looks around the whole index.
seed?number | bigintEqual seeds, shown keys and focus over an unchanged index draw equally.
expansionFactor?numberBreadth of the candidates of the walks behind the draws; absent lets the engine derive it.
DenseGetConfigOptional settings of a retrieval.
usearch.ts:149 ↗
interface DenseGetConfig

Fields

scalar?ScalarThe scalar kind to export as, converted from storage; defaults to f32.
DenseCursorNextConfigOptional settings of one cursor pull.
usearch.ts:165 ↗
interface DenseCursorNextConfig

Fields

links?booleanWhether to export the keys each entry links to beside it; absent pulls the keys alone.
DenseSearchStreamInitConfigOptional settings of a resumable search stream.
usearch.ts:193 ↗
interface DenseSearchStreamInitConfig

Fields

expansionFactor?numberBreadth of the candidates the walk explores; absent lets the engine derive it.
ClassDenseVectorsRows a call wrote into its arena: the entries get exported, or the rows attend attended.6
dense_index.ts:375 ↗
class DenseVectors extends ArenaView

data is a Float64Array, Float32Array, Float16Array or Int8Array for the kinds JavaScript spells, a Uint16Array for bf16 and for f16 on a runtime without Float16Array, and the raw bytes in a Uint8Array for the rest.

DenseVectorsMade by a verb, never directly.
dense_index.ts:387 ↗
new DenseVectors(outputs: CompiledVectors, scalar: Scalar)
rowsRows written; zero when get found no entry under the key.
dense_index.ts:377 ↗
readonly rows: number
columnsValues in each row, as the call reported them.
dense_index.ts:379 ↗
readonly columns: number
strideBytes between consecutive rows.
dense_index.ts:381 ↗
readonly stride: number
scalarThe scalar kind the rows were exported as.
dense_index.ts:383 ↗
readonly scalar: Scalar
dataEvery row, laid stride bytes apart.
dense_index.ts:397 ↗
readonly data: Vector
ClassDenseDrawsThe draws a sample wrote, viewed inside its arena: each draw's key, which of that key's entries it is, the shown or earlier draw it hangs from, and where it is placed.6
dense_index.ts:495 ↗
class DenseDraws extends ArenaView
DenseDrawsMade by sample, never directly.
dense_index.ts:506 ↗
new DenseDraws(outputs: CompiledDraws)
drawsDraws written; fewer than wanted only once the region is exhausted.
dense_index.ts:497 ↗
readonly draws: number
parentsEach draw's parent and its distance, one column per draw, a root holding none.
dense_index.ts:499 ↗
readonly parents: Matches
coordinatesWhere each draw is placed, one row of three f32 values apiece.
dense_index.ts:501 ↗
readonly coordinates: DenseVectors
keysSixteen bytes per draw.
dense_index.ts:516 ↗
readonly keys: Uint8Array
entryOrdinalsWhich of its key's entries each draw is, in the order get writes them.
dense_index.ts:521 ↗
readonly entryOrdinals: BigUint64Array
ClassDenseCursorBatchOne cursor pull, viewed inside its arena: the keys, and the keys they link to when those were asked for.4
dense_index.ts:527 ↗
class DenseCursorBatch extends ArenaView
DenseCursorBatchMade by DenseCursor.next, never directly.
dense_index.ts:535 ↗
new DenseCursorBatch(outputs: CompiledBatch)
entriesEntries pulled; zero once the walk is exhausted.
dense_index.ts:529 ↗
readonly entries: number
keysSixteen bytes per entry pulled.
dense_index.ts:543 ↗
readonly keys: Uint8Array
ClassDenseCursorA walk over an index.3
dense_index.ts:556 ↗
class DenseCursor implements Disposable

Each shard's publication watermark is read as the walk enters it, so an entry once passed is never revisited.

Holds engine state until free is called; a walk left to the garbage collector releases it whenever the collector gets to it. It keeps its index alive, and pulls on the executor of the receiver that opened it.

DenseCursorMade by cursor, never directly.
dense_index.ts:560 ↗
new DenseCursor(compiled: CompiledCursor)
nextPulls the next entries of the walk into arena; the vectors themselves come from get.
dense_index.ts:572 ↗
next(
    wanted: number,
    arena: Arena,
    config: DenseCursorNextConfig = {},
): DenseCursorBatch

Parameters

wantednumberThe upper bound on entries this pull lands.
arenaArenaThe memory the batch lands in, rewound by the pull.
configDenseCursorNextConfigWhether to export each entry's links; absent pulls keys alone.

Returns

The batch; zero entries once the walk is exhausted.
freeReleases the scan; a second call does nothing.
dense_index.ts:577 ↗
free(): void
ClassDenseSearchStreamA resumable nearest-neighbor walk over one query, pulled from the caller's own thread.3
dense_index.ts:594 ↗
class DenseSearchStream implements Disposable

This is the filtered path: pull matches, drop the ones a JavaScript predicate rejects, and pull again until satisfied - rather than guessing a k wide enough to survive the filter. The stream keeps walk state rather than a thread, and every pull is an ordinary call on the executor of the receiver that opened it.

DenseSearchStreamMade by searchStream, never directly.
dense_index.ts:598 ↗
new DenseSearchStream(compiled: CompiledSearchStream)
nextPulls the next-nearest matches from the stream into arena.
dense_index.ts:609 ↗
next(wanted: number, arena: Arena): Matches

Parameters

wantednumberThe upper bound on matches this pull lands.
arenaArenaThe memory the matches land in, rewound by the pull.

Returns

One query's ranked matches, whose count is zero once the walk is drained.
freeReleases the walk state; a second call does nothing.
dense_index.ts:614 ↗
free(): void