High-performance approximate nearest neighbor index
Public fields
dimension
Vector dimension
metric
Distance metric
n_trees
Number of trees (for Annoy)
search_k
Search parameter
Methods
Method new()
Create a new HNSWIndex
Usage
HNSWIndex$new(dimension, metric = "angular", n_trees = 50, search_k = -1)
Arguments
dimension
Vector dimension
metric
Distance metric: "angular", "euclidean", "manhattan", "dot"
n_trees
Number of trees for index (higher = more accuracy)
search_k
Search parameter (higher = more accuracy, -1 = auto)
Method add_items()
Add items to the index
Usage
HNSWIndex$add_items(ids, vectors)
Arguments
ids
Character vector of IDs
vectors
Matrix of vectors (rows = items)
Method build()
Build the index (required before searching)
Search for nearest neighbors
Usage
HNSWIndex$search(query, k = 10, include_distances = TRUE)
Arguments
query
Query vector
k
Number of neighbors
include_distances
Return distances
Returns
Data frame with id, distance columns
Method get_vector()
Get vector by ID
Method get_ids()
Get all IDs
Method size()
Get item count
Method remove_items()
Remove items from index
Usage
HNSWIndex$remove_items(ids)
Method clear()
Clear the index
Method clone()
The objects of this class are cloneable with this method.
Usage
HNSWIndex$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.
Examples
if (FALSE) { # \dontrun{
# Create index
index <- HNSWIndex$new(dimension = 128, metric = "angular")
# Add vectors
index$add_items(ids = c("a", "b", "c"),
vectors = matrix(rnorm(384), nrow = 3))
# Search
results <- index$search(query = rnorm(128), k = 5)
} # }