Mojo · Quickstart

Quickstart

3.0.0-rc.1 · 4c3e5023

from usearch import Arena, DenseIndex, DenseMetric, DenseOptions, Executor, Key, Library, RowsView, ScalarKind

def main() raises:
    var library = Library()
    var options = DenseOptions(3, DenseMetric.ANGULAR, ScalarKind.BF16)  # the defaults, spelled out
    var index = DenseIndex.init_ram(library, options)  # one shard on every NUMA node
    index.reserve(1_000)

    var keys = [Key(integer=42), Key(integer=43)]
    var vectors = [Float32(0.2), 0.6, 0.4, Float32(0.1), 0.3, 0.9]
    index.insert(keys, RowsView.of(vectors, 3))  # on the calling thread

    var executor = Executor.for_ram(library, threads=4)
    var arena = Arena(executor)
    var query = [Float32(0.2), 0.6, 0.4]
    var matches = index.on(executor).search(RowsView.of(query, 3), 10, arena)  # the same verb, on an executor
    print(matches.query_keys(0)[0], matches.query_distances(0)[0])  # 00000000-0000-0000-0000-00000000002a 0.0

An index owns entries; an Executor owns the threads or streams a call runs on; an Arena owns the outputs a call writes. Library() opens the engine once, and every handle created through it keeps it mapped.