Anvia
Retrieval

LSH

Use locality-sensitive hashing helpers for retrieval workflows.

Locality-sensitive hashing narrows the candidate set before scoring vectors. Use it when an in-memory store has enough documents that brute-force search is too slow.

1. Start With the Default

const store = InMemoryVectorStore.fromDocuments(embedded);

The default search strategy is brute force. It is simpler and a good starting point for tests, small datasets, and early prototypes.

2. Enable LSH

const store = InMemoryVectorStore.fromDocuments(embedded, {
  index: {
    type: "lsh",
    numTables: 4,
    numHyperplanes: 8,
    seed: 7,
  },
});

const index = store.index(embeddings);

LSH trades exact candidate selection for faster narrowing. Results are still scored after candidates are selected.

3. Search Normally

const results = await index.search({
  query: "password reset",
  topK: 3,
});

The search API is the same as the default index.

4. Tune Carefully

OptionEffect
numTablesMore tables can improve recall with more memory
numHyperplanesMore hyperplanes make buckets narrower
seedMakes the index deterministic for tests

For production-scale retrieval, use a durable vector database. Anvia's in-memory LSH is best for local workflows and lightweight deployments.