Rust-accelerated exact and approximate nearest neighbour search for R. Build an index once, keep it in memory behind an R6 object and query it as often as you like. The backbone is the Rust crate ann-search-rs.
Overview
Indices
Thirteen CPU indices, all behind the same $new() / $predict() interface:
- Exact: brute force (SIMD, and Apple Accelerate on macOS) and kMkNN
- Trees: Annoy, k-d forest and ball tree
- Partitions: IVF, SOAR and LSH
- Graphs: HNSW, NN-Descent, Vamana, NSG and RNN-Descent
Euclidean, squared Euclidean, cosine and Manhattan distances, f32 or f64 storage, multi-threaded via Rayon. Indices can be saved to disk and loaded back, and $query_self() gives you the kNN graph of the indexed data itself.
Speed
At equal recall on the ann-benchmarks datasets, annsearchR’s HNSW runs about 2x the queries per second of RcppHNSW with 10 threads and 3.6 to 4x on one thread. NN-Descent is 4.5 to 23x faster than rnndescent and Annoy 1.5 to 3.7x faster than RcppAnnoy. The full comparison against RcppHNSW, RcppAnnoy, BiocNeighbors, rnndescent, FNN and RANN on Fashion-MNIST and SIFT-128 is in the benchmarks article.
Installation
CRAN
install.packages("annsearchR")CRAN ships binaries for Windows and macOS, so no Rust needed there. On Linux it builds from source, and the source build needs rustc 1.90 or newer (see below).
r-universe
r-universe.dev builds every commit to main as a pre-compiled binary, so you get fixes before they hit CRAN.
install.packages(
'annsearchR',
repos = c('https://gregorlueg.r-universe.dev', 'https://cloud.r-project.org')
)From source
That compiles all of the Rust crates from scratch, so you need Rust first.
macOS and Linux:
Windows:
Download and run the installer from rustup.rs.
Restart your terminal, check rustc --version reports 1.90 or newer, then:
remotes::install_github("GregorLueg/annsearchR")How to use the package … ?
library(annsearchR)
dat <- generate_clustered_data(20000L, 32L)$data
hnsw <- HnswIndex$new(dat[-(1:500), ], metric = "cosine")
res <- hnsw$predict(dat[1:500, ], k = 10L)For everything else, please check out the website and the getting started vignette.
License
Copyright (c) 2026 annsearchR authors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
