Python · Retrieval and Streaming

Retrieval and Streaming

from usearch import DenseVectors, Sample, DenseBatch, … 3.0.0-rc.1 · 4c3e5023

index.get(42, arena)  # a (count, dimensions) `DenseVectors` of what is stored under it
cursor = index.on(pool).cursor()  # entry scan, pulled in batches; `cursor([42])` walks out from key 42
while batch := cursor.next(1024, arena, links=True):
    ...  # batch.keys, and batch.links as (offsets, keys) when asked; vectors come from `get`
with index.on(pool).search_stream(query) as stream:  # resumable best-first stream
    while pulled := stream.next(16, arena)[0]:  # one query of `Matches`, empty once drained
        if pulled[-1].distance > 0.3:
            break

get for an absent key answers an empty, falsy DenseVectors. Every pull names its arena and rewinds it like any other call, so one arena serves a scan and any other verb in turn. A drained cursor answers an empty, falsy DenseBatch, and a drained stream a query with no matches. A cursor or a stream releases its walk on close() or at the end of a with block, so an absorb waiting on it need not wait for the collector, and a later pull raises UninitializedError.

§ Sampling

drawn = index.on(pool).sample(256, arena, shown=shown, shown_coordinates=xyz, focus=near, seed=42)

sample draws keys around focus, each hanging from a shown or earlier draw, and places them in three dimensions beside the shown keys. The Sample it answers holds keys, entry_ordinals, parents and coordinates in the arena, and a larger wanted under the same seed draws a superset.

API

ClassDenseVectorsThe rows get or attend wrote into an Arena: (count, dimensions), or (count, row_bytes) of 'B'.6
lib.pyi:494 ↗
class DenseVectors
__buffer__Host memory only; device memory raises DeviceError, a rewound arena UninitializedError.
lib.pyi:481 ↗

Inherited from _ArenaBuffer

def __buffer__(flags: int, /) -> memoryview

Python-only.

__len__Rows, or bytes of a Monolith.
lib.pyi:485 ↗

Inherited from _ArenaBuffer

def __len__() -> int

Python-only.

__arrow_c_array__Exports host rows as an Arrow schema and array capsule.
lib.pyi:117 ↗

Inherited from _ArrowOutput

def __arrow_c_array__(
    requested_schema: _Capsule | None = None,
) -> tuple[_Capsule, _Capsule]
__arrow_c_device_array__Exports rows wherever they live as an Arrow schema and device array capsule.
lib.pyi:121 ↗

Inherited from _ArrowOutput

def __arrow_c_device_array__(
    requested_schema: _Capsule | None = None,
) -> tuple[_Capsule, _Capsule]
__arrow_c_stream__Exports host rows as an Arrow stream of one chunk.
lib.pyi:125 ↗

Inherited from _ArrowOutput

def __arrow_c_stream__(
    requested_schema: _Capsule | None = None,
) -> _Capsule
__cuda_array_interface__Version 3 of the CUDA Array Interface, present only over device memory.
lib.pyi:490 ↗

Inherited from _ArenaBuffer

__cuda_array_interface__: dict[str, Any]

Python-only.

ClassSampleThe draws one sample call wrote into an Arena, viewing it until it rewinds.4
lib.pyi:1798 ↗
class Sample(structseq[Any], tuple[_ArenaBuffer, _ArenaBuffer, Matches, _ArenaBuffer])
keys(written, 16) identities drawn, most significant byte first.
lib.pyi:1805 ↗
keys: _ArenaBuffer
entry_ordinals(written,) which of its key's entries each draw is, in the order get writes them.
lib.pyi:1810 ↗
entry_ordinals: _ArenaBuffer
parentsOne column per draw: the shown or earlier draw it hangs from, and its distance.
lib.pyi:1815 ↗
parents: Matches
coordinates(written, 3) float32 places of the draws, beside the shown keys.
lib.pyi:1820 ↗
coordinates: _ArenaBuffer
ClassDenseBatchThe entries one cursor step wrote into an Arena; empty, so falsy, once the scan is drained.2
lib.pyi:1850 ↗
class DenseBatch
keys(count, 16) identities, most significant byte first.
lib.pyi:1856 ↗
keys: _ArenaBuffer
ClassDenseCursorA sequential scan over the entries an index holds, pulled in batches by next.2
lib.pyi:1867 ↗
class DenseCursor
nextThe next batch of at most wanted entries, in arena.
lib.pyi:1872 ↗
def next(
    wanted: int,
    arena: Arena,
    /,
    *,
    links: bool = False,
) -> DenseBatch
closeReleases the scan before the cursor is collected.
lib.pyi:1876 ↗
def close() -> None
ClassDenseSearchStreamA resumable best-first stream of matches for one query, pulled by next.2
lib.pyi:1885 ↗
class DenseSearchStream
nextThe next at most wanted matches, as one query in arena.
lib.pyi:1890 ↗
def next(wanted: int, arena: Arena, /) -> Matches
closeReleases the walk before the stream is collected.
lib.pyi:1894 ↗
def close() -> None