@saehrimnir/druidjs / Annoy
Class: Annoy<T>
Defined in: knn/Annoy.js:45
Annoy-style (Approximate Nearest Neighbors Oh Yeah) implementation using Random Projection Trees.
This implementation builds multiple random projection trees where each tree randomly selects two points and splits the space based on a hyperplane equidistant between them.
Key features:
- Multiple random projection trees for better recall
- Each tree uses random hyperplanes for splitting
- Priority queue search for better recall
- Combines results from all trees
Best suited for:
- High-dimensional data
- Approximate nearest neighbor search
- Large datasets
- When high recall is needed with approximate methods
Template
T
See
- https://github.com/spotify/annoy
- https://erikbern.com/2015/09/24/nearest-neighbors-and-vector-models-epilogue-curse-of-dimensionality.html
Extends
KNN
Type Parameters
| Type Parameter | Description |
|---|---|
T extends number[] | Float64Array |
Constructors
Constructor
new Annoy<T>(elements: T[], parameters?: Partial<ParametersAnnoy>): Annoy<T>;Defined in: knn/Annoy.js:53
Creates a new Annoy-style index with random projection trees.
Parameters
| Parameter | Type | Description |
|---|---|---|
elements | T[] | Elements to index |
parameters? | Partial<ParametersAnnoy> | Anything left out falls back to the documented default. |
Returns
Annoy<T>
Overrides
KNN.constructorProperties
| Property | Type | Inherited from | Defined in |
|---|---|---|---|
_elements | T[] | KNN._elements | knn/KNN.js:16 |
_maxPointsPerLeaf | number | - | knn/Annoy.js:76 |
_metric | Metric | - | knn/Annoy.js:74 |
_numTrees | number | - | knn/Annoy.js:75 |
_parameters | ParametersAnnoy | KNN._parameters | knn/KNN.js:18 |
_randomizer | Randomizer | KNN._randomizer | knn/Annoy.js:78 |
_seed | number | - | knn/Annoy.js:77 |
_type | "array" | "typed" | KNN._type | knn/KNN.js:20 |
Accessors
num_nodes
Get Signature
get num_nodes(): number;Defined in: knn/Annoy.js:103
Get the total number of nodes in all trees.
Returns
number
num_trees
Get Signature
get num_trees(): number;Defined in: knn/Annoy.js:95
Get the number of trees in the index.
Returns
number
Methods
add()
add(elements: T[]): Annoy<T>;Defined in: knn/Annoy.js:126
Add elements to the Annoy index.
Parameters
| Parameter | Type | Description |
|---|---|---|
elements | T[] | - |
Returns
Annoy<T>
search()
search(query: T, k?: number): {
distance: number;
element: T;
index: number;
}[];Defined in: knn/Annoy.js:281
Search for k approximate nearest neighbors.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
query | T | undefined | - |
k? | number | 5 | - |
Returns
{ distance: number; element: T; index: number; }[]
Overrides
KNN.searchsearch_by_index()
search_by_index(i: number, k?: number): {
distance: number;
element: T;
index: number;
}[];Defined in: knn/KNN.js:77
Searches the k nearest neighbors of the element stored at index i.
The queried element is never part of the result. It is trivially its own closest neighbor at distance 0, which is never what a caller asking "what is this point near?" wants, so every caller used to strip it back out — each in its own, subtly different way. Note the asymmetry with search: an arbitrary query point has no "self" to exclude, so there k means "k results", while here it means "k neighbors".
The self match is removed by index — not by position, and not by looking for a zero distance. Position is wrong because an approximate index may order ties differently or miss the element altogether (one extra candidate is requested to cover that), and a zero distance is wrong because genuine duplicate points share it and must survive.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
i | number | undefined | Index of the query element. |
k? | number | 5 | Number of neighbors to return. Default is 5 |
Returns
{ distance: number; element: T; index: number; }[]
The k nearest other elements, closest first. Empty when i is out of range.
Inherited from
KNN.search_by_indexsearch_index()
search_index(i: number, k?: number): {
distance: number;
element: T;
index: number;
}[];Defined in: knn/Annoy.js:402
Alias for search_by_index for backward compatibility.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
i | number | undefined | Index of the query element |
k? | number | 5 | Number of nearest neighbors to return |
Returns
{ distance: number; element: T; index: number; }[]