aarondb/vec_index

Types

A single layer in the Hierarchical Navigable Small-World (HNSW) graph.

pub type Layer {
  Layer(edges: dict.Dict(fact.EntityId, List(fact.EntityId)))
}

Constructors

A search result: entity ID + similarity score.

pub type SearchResult {
  SearchResult(entity: fact.EntityId, score: Float)
}

Constructors

A Hierarchical Navigable Small-World (HNSW) graph for approximate nearest neighbor search. Scales logarithmically even for million-scale datasets by using hierarchical layers.

pub type VecIndex {
  VecIndex(
    nodes: dict.Dict(fact.EntityId, List(Float)),
    layers: dict.Dict(Int, Layer),
    max_neighbors: Int,
    entry_point: Result(fact.EntityId, Nil),
    max_level: Int,
  )
}

Constructors

Values

pub fn contains(idx: VecIndex, entity: fact.EntityId) -> Bool

Check if the index contains a given entity.

pub fn delete(idx: VecIndex, entity: fact.EntityId) -> VecIndex

Remove a node from the index across all layers and repair edges.

pub fn insert(
  idx: VecIndex,
  entity: fact.EntityId,
  vec: List(Float),
) -> VecIndex

Insert a vector into the NSW graph. Greedy-links the new node to its nearest existing neighbors across multiple levels.

pub fn new() -> VecIndex

Create an empty vector index with default max_neighbors of 16.

pub fn new_with_m(m: Int) -> VecIndex

Create an empty vector index with custom max_neighbors.

pub fn search(
  idx: VecIndex,
  query: List(Float),
  threshold: Float,
  k: Int,
) -> List(SearchResult)

Search for vectors similar to query within threshold, returning up to k results. Uses hierarchical greedy beam search.

pub fn size(idx: VecIndex) -> Int

Get the number of vectors in the index.

Search Document