class

USearch::Index

Inherits Reference < Object

High-level wrapper for a USearch HNSW index.

The index stores vectors and allows fast approximate nearest neighbor search. Each vector is associated with a 64-bit key (typically your database row ID).

Constants

DEFAULT_CONNECTIVITY = 16_u64

Default HNSW connectivity parameter (edges per node).

DEFAULT_EXPANSION_ADD = 128_u64

Default expansion factor during index construction.

DEFAULT_EXPANSION_SEARCH = 64_u64

Default expansion factor during search.

Constructors

from_bytes(bytes : Bytes, dimensions : Int, metric : MetricKind = :cos, quantization : ScalarKind = :f16) : Index

Loads an index from a byte buffer.

The buffer is copied, so you can free it after this call.

Source
load(path : String, dimensions : Int, metric : MetricKind = :cos, quantization : ScalarKind = :f16) : Index

Loads an index from a file.

The file must have been saved with #save. You must provide the dimensions since they're needed to create the index before loading.

Source
new(dimensions : Int, metric : MetricKind = :cos, quantization : ScalarKind = :f16, connectivity : Int = DEFAULT_CONNECTIVITY, expansion_add : Int = DEFAULT_EXPANSION_ADD, expansion_search : Int = DEFAULT_EXPANSION_SEARCH, multi : Bool = false)

Creates a new empty index.

  • dimensions: Vector dimensionality (must match all vectors added)
  • metric: Distance metric (default: cosine similarity)
  • quantization: Storage precision (default: f16 for memory efficiency)
  • connectivity: HNSW M parameter (higher = more accurate, more memory)
  • expansion_add: ef_construction parameter
  • expansion_search: ef_search parameter
  • multi: Allow multiple vectors per key
Source
view(path : String, dimensions : Int, metric : MetricKind = :cos, quantization : ScalarKind = :f16) : Index

Memory-maps an index from a file (read-only, memory efficient).

The index is not loaded into memory but accessed directly from disk. This is useful for large indexes that don't fit in RAM.

Source
view_bytes(bytes : Bytes, dimensions : Int, metric : MetricKind = :cos, quantization : ScalarKind = :f16) : Index

Views an index from a byte buffer (zero-copy, read-only).

IMPORTANT: The bytes buffer must remain valid for the lifetime of the index. Do not modify or free the buffer while the index is in use.

Source

Class methods

distance(a : Array(Float32) | Slice(Float32), b : Array(Float32) | Slice(Float32), metric : MetricKind = :cos) : Float32

Computes distance between two vectors without an index.

Source
metadata(path : String) : IndexMetadata

Reads metadata from a saved index file without loading it.

Useful for inspecting index properties before deciding to load.

Source
metadata_buffer(bytes : Bytes) : IndexMetadata

Reads metadata from a serialized index buffer without loading it.

Source
version

Returns the library version.

Source

Instance methods

add(key : UInt64, vector : Array(Float32) | Slice(Float32))

Adds a vector to the index.

  • key: Unique identifier for this vector (e.g., database row ID)
  • vector: The vector data (must match index dimensions)

Vectors are passed as Float32 and converted to the index's quantization format.

Source
capacity

Returns the current capacity (number of vectors that fit without reallocation).

Source
clear

Clears all vectors from the index.

Source
close

Closes the index and frees resources.

Source
closed?

Returns true if the index has been closed.

Source
connectivity

Returns the HNSW connectivity parameter (M).

Source
contains?(key : UInt64) : Bool

Checks if a key exists in the index.

Source
count(key : UInt64) : UInt64

Returns the number of vectors for a key (useful in multi-mode).

In single-mode, returns 1 if key exists, 0 otherwise.

Source
dimensions

Returns the vector dimensionality.

Source
expansion_add

Returns the current expansion factor for add operations.

Source
expansion_add=(value : Int)

Sets the expansion factor for add operations.

Higher values = better graph quality but slower indexing. Default is 128.

Source
expansion_search

Returns the current expansion factor for search operations.

Source
expansion_search=(value : Int)

Sets the expansion factor for search operations.

Higher values = more accurate but slower. Default is 64.

Source
filtered_search(query : Array(Float32) | Slice(Float32), k : Int = 10, &filter : UInt64 -> Bool) : Array(SearchResult)

Searches for the k nearest neighbors with a filter predicate.

  • query: The query vector (must match index dimensions)
  • k: Maximum number of neighbors to return
  • &filter: Block that receives a key and returns true to include it

Example:

# Only return vectors with even keys
results = index.filtered_search(query, k: 10) { |key| key.even? }

# Only return vectors in a specific set
valid_ids = Set{1_u64, 5_u64, 10_u64}
results = index.filtered_search(query, k: 10) { |key| valid_ids.includes?(key) }
Source
finalize

Ensures the index is closed when garbage collected. Exceptions are swallowed since finalizers run during GC.

Source
get(key : UInt64) : Array(Float32) | Nil

Retrieves the vector data for a key.

Returns nil if the key doesn't exist.

Source
memory_usage

Returns memory usage in bytes.

Source
metric=(metric : MetricKind)

Changes the distance metric at runtime.

Source
remove(key : UInt64)

Removes a vector by key.

Source
rename(from : UInt64, to : UInt64)

Renames a key (changes the ID associated with a vector).

Source
reserve(capacity : Int)

Pre-allocates space for the given number of vectors.

Source
save(path : String)

Saves the index to a file.

Source
search(query : Array(Float32) | Slice(Float32), k : Int = 10) : Array(SearchResult)

Searches for the k nearest neighbors to a query vector.

  • query: The query vector (must match index dimensions)
  • k: Number of neighbors to return (default: 10)

Returns an array of SearchResult with keys and distances, sorted by distance.

Source
serialized_length

Returns the serialized size of the index in bytes.

Source
set_custom_metric(callback : LibUSearch::MetricCallback, metric_kind : MetricKind = :unknown)

Sets a custom distance metric function (advanced).

The callback receives two raw vector pointers and returns a distance. You must know the vector dimensions and handle the pointer arithmetic.

NOTE: The callback does not receive state, so you cannot use closures that capture variables. Use module-level functions or constants.

Source
size

Returns the number of vectors in the index.

Source
threads_add=(value : Int)

Sets the number of threads for add operations.

Source
threads_search=(value : Int)

Sets the number of threads for search operations.

Source
to_bytes

Serializes the index to a byte buffer.

Returns a new Bytes containing the serialized index.

Source