Ismail Ben Alla

Tiny NN Search

Vector and Keyword Search from Scratch

RAG · Nov 2026 · In progress

Exact, approximate and keyword search written from scratch in NumPy, benchmarked against faiss, hnswlib and rank_bm25.

Built withNumPyGradioPyTorchMatplotlibpytest

Tiny NN Search — overview

Overview

Tiny NN Search is a vector and keyword search engine I wrote from scratch in NumPy. Every index has the same interface: add vectors, search the top k, save and load.

On top of the indexes, a search engine handles text, images and audio. Each query is searched with its own kind of model, so a picture finds similar pictures and a sound finds similar sounds. A CLIP- or CLAP-style model adds text queries.

It is the retrieval engine of tiny-rag, and it loads the trained text, image and audio embedders of tiny-embedder from the family's shared model store.

The question

How close can from-scratch NumPy indexes get to faiss and hnswlib in recall and speed, on one CPU thread?

Key features

How it works

  1. Indexes

    IVF clusters the vectors with k-means and only scans the nearest lists. PQ compresses vectors into codes and scores them with lookup tables. HNSW walks a layered graph. LSH hashes with random hyperplanes and probes nearby buckets.

  2. Keywords and hybrid

    BM25 scores documents from an inverted index. Hybrid search fuses the BM25 and dense rankings, by reciprocal rank or by a weighted score.

  3. One engine per modality

    The engine tells the query's modality from its type (text, image file, array, waveform) and embeds it with the matching model, then searches the index and returns the stored payloads.

Examples

Recall@10 against queries per second on GloVe-100 (200,000 vectors, one CPU thread): my indexes in solid lines, faiss and hnswlib dashed.
Recall@10 against queries per second on GloVe-100 (200,000 vectors, one CPU thread): my indexes in solid lines, faiss and hnswlib dashed.
SciFact nDCG@10: my BM25, rank_bm25, the hashing dense baseline and the hybrid fusions.
SciFact nDCG@10: my BM25, rank_bm25, the hashing dense baseline and the hybrid fusions.
Search by example with the baseline embedders: precision@10 on CIFAR-10, ESC-50 and Free Spoken Digits, exact search and HNSW.
Search by example with the baseline embedders: precision@10 on CIFAR-10, ESC-50 and Free Spoken Digits, exact search and HNSW.
The Gradio test app: text, image and audio search, and the index lab.
The Gradio test app: text, image and audio search, and the index lab.

Tech stack

What I learned

Limitations

Related projects