Vector Search Without Brute Force: How IVF and HNSW Work
Exact search in high dimensions freezes applications. Two algorithms solve the problem: IVF splits the space into clusters (k-means), while HNSW builds a neighborhood graph. Habr has a detailed breakdown with a Python implementation and code examples.
AI-processed from Habr AI; edited by Hamidun News
When a vector database holds millions of embeddings, finding similar vectors by brute force becomes impractical. Two algorithms, IVF and HNSW, solve the problem with approximate search, returning an answer in milliseconds instead of minutes. A new detailed article on Habr walks through both approaches, from the underlying math to a Python implementation.
Why exact search doesn't work
In low dimensions (2D, 3D), you can build spatial indexes such as k-d trees and R-trees. They rely on geometry: if the query is far from one region of the space, that region can be excluded from the search without losing any results.
In high dimensions (768D for BERT, 3072D for GPT-4 Turbo), this idea breaks down. Distances become non-local: almost all points sit at nearly the same distance from each other. This phenomenon is known as the curse of dimensionality. Traditional indexes lose their efficiency and stop narrowing the search space, so we are back to brute force: comparing the query against all millions of vectors in the database.
IVF: clustering to the rescue
IVF (Inverted File Index) tackles the problem with up-front clustering. The algorithm uses classic k-means to split the space into notional "buckets", or clusters. The intuition is simple: if vectors are close in space, they should land in the same cluster, so a search only needs to check a few relevant clusters rather than the whole database.
The process has two stages. During indexing, each vector in the database is assigned to its nearest cluster (its centroid). During search, the system first finds the K clusters closest to the query using a fast lookup, then looks for the exact match only inside them. This sharply cuts the number of comparisons: a few thousand instead of millions.
- First stage: k-means splits the space into hundreds or thousands of clusters
- Second stage: each database vector is indexed into its cluster (the centroid ID is stored)
- At search time: find the K nearest clusters, then run a full search only inside them
IVF scales well to billions of vectors and needs relatively little memory. Habr's article walks through a Python implementation with concrete code examples and performance metrics.
HNSW: a hierarchical neighborhood graph
HNSW (Hierarchical Navigable Small World) takes a completely different approach: it builds a neighborhood graph instead of clusters. Each vector becomes a node, and edges connect neighboring (close) vectors. The graph is hierarchical: the lower levels hold the dense local neighborhood of each vector, while the upper levels hold sparse long-range links for fast, coarse global descent.
During search, the algorithm starts at an entry point on the top level and descends through the hierarchy, picking at each step the neighbors closest to the target query. It's like navigating a big city: you start on the highways, then switch to local streets until you find the right address.
"HNSW works like a navigator in an unfamiliar city: you start on the main highways, then descend to local streets," the authors of the
Habr article explain.
In practice, HNSW is usually faster than IVF for low-latency search, but it needs more memory to store the graph structure and the links between nodes.
What this means
Both algorithms are used in modern vector databases: Pinecone, Weaviate, Qdrant and Milvus. For LLM applications such as RAG systems, document search, semantic recommendations and intelligent chatbots, the choice of search algorithm directly affects response speed and user experience.
IVF is the better choice for huge databases with limited memory. HNSW has the edge when minimal latency is required and when working with more compact datasets.
- Oct 27, 2026Weaviate 1.38: HFresh Disk Index and MCP Server Go Stable
- May 31, 2026Vector Search in PostgreSQL: A Complete pgvector Guide for AI Applications
- Oct 29, 2026pgvector vs Qdrant: a benchmark on 100 000 vectors with 1536 dimensions
- Oct 13, 2026Machine Learning Mastery shows how to build RAG on scikit-learn without a vector database
Key Questions Answered
Why exact search doesn't work?
In low dimensions (2D, 3D), you can build spatial indexes such as k-d trees and R-trees. They rely on geometry: if the query is far from one region of the space, that region can be excluded from the search without losing any results. In high dimensions (768D for BERT, 3072D for GPT-4 Turbo), this idea breaks down. Distances become non-local: almost all points sit at nearly the same distance from each other. This phenomenon is known as the curse of dimensionality.
What are the key details on IVF: clustering to the rescue?
IVF (Inverted File Index) tackles the problem with up-front clustering. The algorithm uses classic k-means to split the space into notional "buckets", or clusters. The intuition is simple: if vectors are close in space, they should land in the same cluster, so a search only needs to check a few relevant clusters rather than the whole database. The process has two stages. During indexing, each vector in the database is assigned to its nearest cluster (its centroid).
What are the key details on HNSW: a hierarchical neighborhood graph?
HNSW (Hierarchical Navigable Small World) takes a completely different approach: it builds a neighborhood graph instead of clusters. Each vector becomes a node, and edges connect neighboring (close) vectors. The graph is hierarchical: the lower levels hold the dense local neighborhood of each vector, while the upper levels hold sparse long-range links for fast, coarse global descent.
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
Daily 60-sec AI Voice Brief + Weekly Production AI Agent Playbook
Listen to a 60-second AI news brief every morning and get a weekly hands-on playbook for shipping AI agents to production — tools, MCP servers and patterns that work. Curated by Jemal Hamidun, Head of AI at Alpina Digital.
