Skip to content

@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 ​

Extends ​

  • KNN

Type Parameters ​

Type ParameterDescription
T extends number[] | Float64Array

Constructors ​

Constructor ​

ts
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 ​

ParameterTypeDescription
elementsT[]Elements to index
parameters?Partial<ParametersAnnoy>Anything left out falls back to the documented default.

Returns ​

Annoy<T>

Overrides ​

ts
KNN.constructor

Properties ​

PropertyTypeInherited fromDefined in
_elementsT[]KNN._elementsknn/KNN.js:16
_maxPointsPerLeafnumber-knn/Annoy.js:76
_metricMetric-knn/Annoy.js:74
_numTreesnumber-knn/Annoy.js:75
_parametersParametersAnnoyKNN._parametersknn/KNN.js:18
_randomizerRandomizerKNN._randomizerknn/Annoy.js:78
_seednumber-knn/Annoy.js:77
_type"array" | "typed"KNN._typeknn/KNN.js:20

Accessors ​

num_nodes ​

Get Signature ​

ts
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 ​

ts
get num_trees(): number;

Defined in: knn/Annoy.js:95

Get the number of trees in the index.

Returns ​

number

Methods ​

add() ​

ts
add(elements: T[]): Annoy<T>;

Defined in: knn/Annoy.js:126

Add elements to the Annoy index.

Parameters ​

ParameterTypeDescription
elementsT[]-

Returns ​

Annoy<T>


ts
search(query: T, k?: number): {
  distance: number;
  element: T;
  index: number;
}[];

Defined in: knn/Annoy.js:281

Search for k approximate nearest neighbors.

Parameters ​

ParameterTypeDefault valueDescription
queryTundefined-
k?number5-

Returns ​

{ distance: number; element: T; index: number; }[]

Overrides ​

ts
KNN.search

search_by_index() ​

ts
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 ​

ParameterTypeDefault valueDescription
inumberundefinedIndex of the query element.
k?number5Number 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 ​

ts
KNN.search_by_index

search_index() ​

ts
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 ​

ParameterTypeDefault valueDescription
inumberundefinedIndex of the query element
k?number5Number of nearest neighbors to return

Returns ​

{ distance: number; element: T; index: number; }[]