lsh

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package lsh provides locality-sensitive hashing for approximate nearest neighbor search.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateHyperplanes

func GenerateHyperplanes(seed int64, numBits, dimension int) [][]float64

GenerateHyperplanes generates random normalized hyperplanes from a seed. This should only be called by the enterprise admin during setup, NOT by clients during search.

The hyperplanes are normalized to unit length to ensure consistent hashing behavior.

func GenerateHyperplanesFromBytes

func GenerateHyperplanesFromBytes(seed []byte, numBits, dimension int) [][]float64

GenerateHyperplanesFromBytes generates hyperplanes from a byte seed. Useful when the seed comes from a cryptographically random source.

func GenerateSessionKey

func GenerateSessionKey(numBytes int) []byte

GenerateSessionKey generates a random session key for hash masking.

func HammingDistance

func HammingDistance(a, b []byte) int

HammingDistance computes the Hamming distance between two byte slices.

func HashToIndex

func HashToIndex(hash []byte, numBuckets int) int

HashToIndex converts a hash to a bucket index in range [0, numBuckets). Uses the hash bytes directly to compute a bucket index. This is useful for hierarchical indexing where we need to map vectors to a fixed number of buckets.

func MaskHash

func MaskHash(hash []byte, sessionKey []byte) []byte

MaskHash XORs the hash with a session key for privacy. This prevents the server from correlating queries across sessions.

func UnmaskHash

func UnmaskHash(maskedHash []byte, sessionKey []byte) []byte

UnmaskHash reverses the masking operation.

Types

type Candidate

type Candidate struct {
	ID       string
	Distance int // Hamming distance
}

Candidate represents a search result candidate

type Config

type Config struct {
	Dimension int   // Vector dimension
	NumBits   int   // Number of hash bits (64, 128, 256, etc.)
	Seed      int64 // Random seed for reproducibility
}

Config holds configuration for creating a new index

type EnterpriseHasher

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

EnterpriseHasher provides LSH hashing using pre-distributed hyperplanes. This is used by clients who receive hyperplanes from the auth service instead of generating them from a seed.

Key security property: The auth service distributes pre-computed hyperplanes instead of seeds. This provides an additional layer of security - even if hyperplanes leak, the original seed remains secret.

func NewEnterpriseHasher

func NewEnterpriseHasher(planes [][]float64) *EnterpriseHasher

NewEnterpriseHasher creates a hasher from pre-generated hyperplanes.

func (*EnterpriseHasher) Dimension

func (h *EnterpriseHasher) Dimension() int

Dimension returns the expected vector dimension.

func (*EnterpriseHasher) GetPlanes

func (h *EnterpriseHasher) GetPlanes() [][]float64

GetPlanes returns the hyperplanes (for serialization).

func (*EnterpriseHasher) Hash

func (h *EnterpriseHasher) Hash(vector []float64) []byte

Hash computes the LSH hash for a vector.

func (*EnterpriseHasher) HashToIndex

func (h *EnterpriseHasher) HashToIndex(vector []float64, numBuckets int) int

HashToIndex computes a bucket index in range [0, numBuckets).

func (*EnterpriseHasher) NumBits

func (h *EnterpriseHasher) NumBits() int

NumBits returns the number of hash bits (hyperplanes).

type Index

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

Index is a locality-sensitive hash index using random hyperplanes.

func Load

func Load(r io.Reader) (*Index, error)

Load deserializes an index from a reader.

func LoadFromFile

func LoadFromFile(path string) (*Index, error)

LoadFromFile loads an index from a file.

func NewIndex

func NewIndex(cfg Config) *Index

NewIndex creates a new LSH index with random hyperplanes.

func (*Index) Add

func (idx *Index) Add(ids []string, vectors [][]float64) error

Add adds vectors to the index.

func (*Index) AddOne

func (idx *Index) AddOne(id string, vector []float64) error

AddOne adds a single vector to the index.

func (*Index) BucketCount

func (idx *Index) BucketCount() int

BucketCount returns the number of buckets.

func (*Index) Count

func (idx *Index) Count() int

Count returns the total number of vectors in the index.

func (*Index) GetPlanes

func (idx *Index) GetPlanes() [][]float64

GetPlanes returns the hyperplanes (for client-side hashing).

func (*Index) GetVector

func (idx *Index) GetVector(id string) ([]float64, bool)

GetVector retrieves a stored vector by ID.

func (*Index) GetVectors

func (idx *Index) GetVectors(ids []string) [][]float64

GetVectors retrieves multiple vectors by ID.

func (*Index) HashBytes

func (idx *Index) HashBytes(vector []float64) []byte

HashBytes returns the LSH hash as bytes (for gRPC transport).

func (*Index) HashToIndexFromVector

func (idx *Index) HashToIndexFromVector(vector []float64, numBuckets int) int

HashToIndexFromVector computes hash and returns bucket index.

func (*Index) MultiProbeSearch

func (idx *Index) MultiProbeSearch(queryHash []byte, k int, numProbes int) ([]Candidate, error)

MultiProbeSearch searches with multi-probe LSH (checks neighboring buckets). This is essentially the same as regular search but with a relaxed distance threshold that increases recall at the cost of precision. The numProbes parameter controls the maximum Hamming distance to include.

func (*Index) Remove

func (idx *Index) Remove(ids []string)

Remove removes vectors from the index by ID.

func (*Index) Save

func (idx *Index) Save(w io.Writer) error

Save serializes the index to a writer.

func (*Index) SaveToFile

func (idx *Index) SaveToFile(path string) error

SaveToFile saves the index to a file.

func (*Index) Search

func (idx *Index) Search(queryHash []byte, k int) ([]Candidate, error)

Search finds k nearest candidates by Hamming distance to the query hash.

func (*Index) SearchVector

func (idx *Index) SearchVector(query []float64, k int) ([]Candidate, error)

SearchVector computes hash and searches in one call.

func (*Index) TwoStageSearch

func (idx *Index) TwoStageSearch(query []float64, initialCandidates, topN int) ([]Candidate, error)

TwoStageSearch performs optimized two-stage retrieval: Stage 1: Get many candidates from LSH (cheap) Stage 2: Rank by Hamming distance, return top N for expensive HE scoring This improves both accuracy (more initial candidates) and speed (fewer HE ops).

type TwoStageConfig

type TwoStageConfig struct {
	InitialCandidates int // Number of candidates from LSH (default: 200)
	TopN              int // Number to return for HE scoring (default: 10)
}

TwoStageConfig holds configuration for two-stage search

func DefaultTwoStageConfig

func DefaultTwoStageConfig() TwoStageConfig

DefaultTwoStageConfig returns sensible defaults

Jump to

Keyboard shortcuts

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