Free · Daily brief + weekly playbookDaily 60-sec AI Voice Brief + Weekly Production AI Agent PlaybookGet the brief →
ReleaseInsight · On RadarHabr AI→ original

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
Vector Search Without Brute Force: How IVF and HNSW Work
Source: Habr AI. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceHabr AI ↗
Editor-in-Chief
First stagek-means splits the space into hundreds or thousands of clusters
Second stageeach database vector is indexed into its cluster (the centroid ID is stored)
At search timefind the K nearest clusters, then run a full search only inside them

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.

Story

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.

ZK
Zhemal Khamidun — Head of AI at Alpina Digital · MIPT (academic supervisor of the Tech Entrepreneurship department). Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

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.

Try Nomad AI →Launch Nomad Agent
Jemal Hamidun

Editor-in-chief · Cite this analysis

Jemal Hamidun

Head of AI at Alpina Digital · Academic Supervisor of the Tech Entrepreneurship Department at MIPT

Head of AI & CPO of AlpinaGPT at Alpina Digital, Academic Lead of Tech Entrepreneurship at MIPT. Founder of Hamidun News and Vibe Academy.

Ready-to-paste citation for journalists, blogs and Telegram channels

Source: Hamidun News — Vector Search Without Brute Force: How IVF and HNSW Work https://news.hamidun.com/en/news/10427/vector-search-without-brute-force-how-ivf-and-hnsw-work

What do you think?
Loading comments…