---
title: "Locality-Sensitive Hashing for Near-Duplicate Detection"
date: "2026-11-22T07:17:12+07:00"
description: "How locality-sensitive hashing puts similar items into the same buckets, and why it is a classic tool for near-duplicate detection at scale."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/locality-sensitive-hashing.jpg"
alt: "Similar items falling into shared hash buckets while dissimilar items scatter"
tags: ['lsh', 'near-duplicate', 'hashing', 'deduplication']
---

## Introduction

Normal hash functions are designed to *spread* similar inputs apart — flip one bit and the hash changes completely. **Locality-sensitive hashing (LSH)** inverts that contract: *similar items are likely to hash to the same bucket*. That single property powers near-duplicate detection, image matching, and fast approximate neighbour search.

## The LSH Contract

A family of hash functions is locality-sensitive if, for any two points `a` and `b`:

- `Pr[h(a) = h(b)]` is **high** when `a` and `b` are close
- `Pr[h(a) = h(b)]` is **low** when `a` and `b` are far

Similarity becomes *collision probability*. You hash everything once, and query time shrinks to "look in the same buckets."

## The Classic Construction: MinHash + Banding

For **near-duplicate text** (Jaccard similarity over shingle sets):

1. **Shingle** each document into word n-grams → a set.
2. **MinHash**: apply many random permutations; the minimum hash value of the set approximates Jaccard similarity. `Pr[minhash(a) = minhash(b)] ≈ Jaccard(a, b)`.
3. **Band the signatures:** split the signature into `b` bands of `r` rows. Two documents are candidates only if *some band matches exactly*.
4. Match probability follows an S-curve: `1 − (1 − s^r)^b` — tune `r` and `b` to place the threshold where you want it (e.g., ~0.8).

Candidates then get exact Jaccard verification. This is the pipeline behind massive-scale deduplication of web crawls.

## LSH for Vectors

For continuous spaces, LSH families target specific metrics:

- **Random projection (SimHash / hyperplane LSH)** for cosine: hash = sign of dot products with random planes; collision ≈ probability the angle is < 90°.
- **p-stable LSH (E2LSH)** for Euclidean distance: project onto random lines with a shift; collisions come from landing in the same bucket.
- **Cross-polytope / multi-probe LSH** — modern variants that probe neighbouring buckets to recover recall.

Search = hash the query, fetch bucket contents, score candidates. Multiple independent hash tables raise recall: a true neighbour only needs to collide in *one* table.

## Strengths and Trade-offs

**Strengths**
- Sub-linear query time with no training step (unlike k-means IVF)
- Natural fit for dedup: exact bucket matches are cheap
- Well-understood theory linking collision probability to similarity

**Trade-offs**
- **Recall is probabilistic** — tuning the S-curve means choosing your false-positive/negative balance
- Many tables/buckets needed for high recall → memory grows
- In benchmarks (see ANN-Benchmarks), LSH generally sits below graph methods on the recall–latency frontier for high-dimensional embeddings — HNSW and IVF tend to win raw search quality

## When to Use LSH

- **Near-duplicate and plagiarism detection** — MinHash is the standard
- **Cheap candidate generation** before a pricier rerank
- **Streaming/one-pass settings** where you need a simple, tunable filter
- For pure top-k ANN on modern embeddings, prefer HNSW/IVF unless you have a specific reason

## Conclusion

Locality-sensitive hashing turns similarity into a hashing problem: make collisions likely for near-neighbours, then let buckets do the pruning. For deduplication it remains the classic, elegant answer; for general vector search it is a useful baseline that modern graph indexes have largely overtaken.

## References

- [Wikipedia: Locality-sensitive hashing](https://en.wikipedia.org/wiki/Locality-sensitive_hashing)
