Skip to content

@saehrimnir/druidjs / LSH

Class: LSH<T>

Defined in: knn/LSH.js:34

Locality Sensitive Hashing (LSH) for approximate nearest neighbor search.

LSH uses hash functions that map similar items to the same buckets with high probability. This implementation uses the p-stable scheme of Datar et al. for Euclidean distance: each hash projects onto a Gaussian random vector and quantizes the result into buckets of width w.

Key concepts:

  • Multiple hash tables increase recall probability
  • Each hash function projects data onto a random Gaussian direction
  • Points landing in the same quantization bucket are hashed together
  • Combines results from all tables for better accuracy

Best suited for:

  • High-dimensional data where exact methods fail
  • Approximate nearest neighbor needs
  • Large datasets where linear scan is too slow
  • When some false positives/negatives are acceptable

Template

T

See

https://en.wikipedia.org/wiki/Locality-sensitive_hashing

Extends

  • KNN

Type Parameters

Type ParameterDescription
T extends number[] | Float64Array

Constructors

Constructor

ts
new LSH<T>(elements: T[], parameters?: Partial<ParametersLSH>): LSH<T>;

Defined in: knn/LSH.js:42

Creates a new LSH index.

Parameters

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

Returns

LSH<T>

Overrides

ts
KNN.constructor

Properties

PropertyTypeInherited fromDefined in
_bucketWidthnumber-knn/LSH.js:82
_dimnumber-knn/LSH.js:78
_elementsT[]KNN._elementsknn/KNN.js:16
_hashTablesMap<string, number[]>[]-knn/LSH.js:66
_metricMetric-knn/LSH.js:58
_numHashFunctionsnumber-knn/LSH.js:60
_numHashTablesnumber-knn/LSH.js:59
_offsetsnumber[][]-knn/LSH.js:74
_parametersParametersLSHKNN._parametersknn/KNN.js:18
_projectionsFloat64Array<ArrayBufferLike>[][]-knn/LSH.js:70
_randomizerRandomizerKNN._randomizerknn/LSH.js:62
_seednumber-knn/LSH.js:61
_type"array" | "typed"KNN._typeknn/KNN.js:20

Methods

add()

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

Defined in: knn/LSH.js:207

Add elements to the LSH index.

Parameters

ParameterTypeDescription
elementsT[]-

Returns

LSH<T>


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

Defined in: knn/LSH.js:255

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