Rust wrappers for NGT, which provides high-speed approximate nearest neighbor
searches against a large volume of data.
Note that NGT will be built dynamically for your target and this requires cmake
.
Furthermore, NGT's shared memory and large dataset features are available through
cargo features shared_mem
and large_data
respectively.
Defining the properties of a new index:
use ngt::{Properties, DistanceType, ObjectType};
let prop = Properties::dimension(3)?;
let prop = Properties::dimension(3)?
.creation_edge_size(10)?
.search_edge_size(40)?
.object_type(ObjectType::Float)?
.distance_type(DistanceType::L2)?;
Creating/Opening an index and using it:
use ngt::{Index, Properties, EPSILON};
let prop = Properties::dimension(3)?;
let index = Index::create("target/path/to/index/dir", prop)?;
let mut index = Index::open("target/path/to/index/dir")?;
let vec1 = vec![1.0, 2.0, 3.0];
let vec2 = vec![4.0, 5.0, 6.0];
let id1 = index.insert(vec1)?;
let id2 = index.insert(vec2)?;
index.build(2)?;
let res = index.search(&vec![1.1, 2.1, 3.1], 1, EPSILON)?;
assert_eq!(res[0].id, id1);
assert_eq!(index.get_vec(id1)?, vec![1.0, 2.0, 3.0]);
index.remove(id1)?;
let res = index.get_vec(id1);
assert!(matches!(res, Result::Err(_)));
let res = index.search(&vec![1.1, 2.1, 3.1], 1, EPSILON)?;
assert_eq!(res[0].id, id2);
assert_eq!(index.get_vec(id2)?, vec![4.0, 5.0, 6.0]);
index.persist()?;