USearch Vector Search Engine

2.24.0 · active · verified Tue Apr 14

USearch is a smaller and faster single-file vector search engine from Unum, offering high-performance approximate nearest neighbor search. It is compact and broadly compatible, focusing on user-defined metrics and fewer dependencies. It follows a rapid release cadence, often with monthly or bi-monthly updates, incorporating bug fixes, performance improvements, and new language bindings across its C++, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram APIs.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a USearch index, add vectors with keys, perform a similarity search to find nearest neighbors, and retrieve vectors by their keys. It uses `numpy` for vector creation and manipulation.

import numpy as np
from usearch.index import Index, Matches

# Initialize an index for 3-dimensional vectors using cosine similarity
index = Index(
    ndim=3,
    metric='cos', # Options include 'l2sq', 'haversine', 'ip' (inner product)
    dtype='f32',  # Quantization to 'f16' (half-precision) or 'i8' (int8) for efficiency
    connectivity=16,
    expansion_add=128,
    expansion_search=64
)

# Create some example vectors
vector1 = np.array([0.2, 0.6, 0.4], dtype=np.float32)
vector2 = np.array([0.1, 0.7, 0.3], dtype=np.float32)
vector3 = np.array([0.9, 0.1, 0.5], dtype=np.float32)

# Add vectors to the index with unique keys
index.add(key=42, vector=vector1)
index.add(key=43, vector=vector2)
index.add(key=44, vector=vector3)

print(f"Index contains {len(index)} vectors.")

# Query the index for the 2 nearest neighbors of vector1
query_vector = np.array([0.21, 0.61, 0.41], dtype=np.float32)
matches: Matches = index.search(query_vector, 2)

print(f"Found {len(matches)} matches for the query:")
for i, match in enumerate(matches):
    print(f"  Match {i+1}: Key={match.key}, Distance={match.distance:.4f}")

# Access a vector by key
retrieved_vector = index[42]
print(f"Retrieved vector for key 42: {retrieved_vector}")
assert np.allclose(retrieved_vector, vector1)

view raw JSON →