2020-12-16 09:17:07 +00:00
|
|
|
![Cover logo](./cover.svg)
|
|
|
|
|
|
|
|
# Instant Distance: fast HNSW indexing
|
|
|
|
|
2021-09-06 14:23:58 +00:00
|
|
|
[![Build status](https://github.com/InstantDomain/instant-distance/workflows/CI/badge.svg)](https://github.com/InstantDomain/instant-distance/actions?query=workflow%3ACI)
|
2020-12-16 09:17:07 +00:00
|
|
|
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE-MIT)
|
|
|
|
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE-APACHE)
|
|
|
|
|
2021-05-25 20:00:51 +00:00
|
|
|
Instance Distance is a fast pure-Rust implementation of the [Hierarchical
|
|
|
|
Navigable Small Worlds paper][paper] by Malkov and Yashunin for finding
|
|
|
|
approximate nearest neighbors. This implementation powers the
|
2024-07-24 18:39:34 +00:00
|
|
|
[Instant Domain Search][domains] backend services used for word vector indexing.
|
2020-12-16 09:17:07 +00:00
|
|
|
|
2021-05-25 20:00:51 +00:00
|
|
|
## What it does
|
|
|
|
|
|
|
|
Instant Distance is an implementation of a fast approximate nearest neighbor
|
|
|
|
search algorithm. The algorithm is used to find the closest point(s) to a given
|
2021-09-06 14:25:14 +00:00
|
|
|
point in a set. As one example, it can be used to make [simple translations][translations].
|
2021-05-25 20:00:51 +00:00
|
|
|
|
|
|
|
## Using the library
|
|
|
|
|
|
|
|
### Rust
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
2021-06-12 02:02:02 +00:00
|
|
|
instant-distance = "0.5.0"
|
2021-05-25 20:00:51 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
```rust
|
|
|
|
use instant_distance::{Builder, Search};
|
|
|
|
|
|
|
|
fn main() {
|
2023-02-21 17:06:30 +00:00
|
|
|
let points = vec![Point(255, 0, 0), Point(0, 255, 0), Point(0, 0, 255)];
|
2021-05-25 20:00:51 +00:00
|
|
|
let values = vec!["red", "green", "blue"];
|
|
|
|
|
|
|
|
let map = Builder::default().build(points, values);
|
|
|
|
let mut search = Search::default();
|
|
|
|
|
|
|
|
let cambridge_blue = Point(163, 193, 173);
|
|
|
|
|
|
|
|
let closest_point = map.search(&cambridge_blue, &mut search).next().unwrap();
|
|
|
|
|
|
|
|
println!("{:?}", closest_point.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
struct Point(isize, isize, isize);
|
|
|
|
|
|
|
|
impl instant_distance::Point for Point {
|
|
|
|
fn distance(&self, other: &Self) -> f32 {
|
|
|
|
// Euclidean distance metric
|
|
|
|
(((self.0 - other.0).pow(2) + (self.1 - other.1).pow(2) + (self.2 - other.2).pow(2)) as f32)
|
|
|
|
.sqrt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Testing
|
|
|
|
|
|
|
|
Rust:
|
|
|
|
|
|
|
|
```
|
|
|
|
cargo t -p instant-distance --all-features
|
|
|
|
```
|
|
|
|
|
|
|
|
Python:
|
|
|
|
|
|
|
|
```
|
|
|
|
make test-python
|
|
|
|
```
|
|
|
|
|
2020-12-16 09:17:07 +00:00
|
|
|
[paper]: https://arxiv.org/abs/1603.09320
|
2024-08-04 19:56:17 +00:00
|
|
|
[domains]: https://instantdomainsearch.com/
|
2024-07-24 18:39:34 +00:00
|
|
|
[translations]: https://instantdomains.com/engineering/how-to-use-fasttext-for-instant-translations
|