lsh

package
v0.0.0-...-79e0e71 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Locality Sensitive Hashing (LSH)

This folder contains an educational implementation of LSH using random hyperplanes (sign hashing) to bucketize vectors for approximate nearest neighbor search.

Key Points

  • Generates numBits random hyperplanes to encode vectors into binary signatures
  • Buckets are addressed by the signature; nearby buckets are explored by flipping bits within a radius
  • Query expands radius until it gathers at least k candidates, then ranks by cosine distance

API (Unified)

  • NewLSHIndex(d, numBits, seed) *LSHIndex
  • Insert(vector []float64) error
  • Search(query []float64, k int) ([]SearchResult, error)
  • GetStats() map[string]interface{}
  • GetNode(id int) interface{}

Usage

See examples/lsh_example.go and the repository root README for a quick start.

Notes

  • Distance metric for ranking is cosine distance (1 - cosine similarity)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLshUtil

func NewLshUtil(seed int64, numBits int) *lshUtil

Types

type LSHIndex

type LSHIndex struct {
	// contains filtered or unexported fields
}

LSHIndex provides a unified interface over lshUtil

func NewLSHIndex

func NewLSHIndex(d int, numBits int, seed int64) *LSHIndex

NewLSHIndex creates a new LSH index with given dimension and bit count seed controls the random hyperplanes generation

func (*LSHIndex) GetNode

func (l *LSHIndex) GetNode(id int) interface{}

GetNode returns the stored vector for a given id

func (*LSHIndex) GetStats

func (l *LSHIndex) GetStats() map[string]interface{}

GetStats returns index statistics

func (*LSHIndex) Insert

func (l *LSHIndex) Insert(vector []float64) error

Insert adds a vector to the index and assigns it an incremental id

func (*LSHIndex) Search

func (l *LSHIndex) Search(query []float64, k int) ([]SearchResult, error)

Search performs approximate k-NN search expanding the bucket radius until at least k candidates are collected

type SearchResult

type SearchResult struct {
	ID       int
	Distance float64
}

SearchResult represents a search result with distance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL