Anvitra - Human-like Search for Modern Applications

5 min read Original article ↗

Public Beta Now Live! Join early adopters testing Anvitra Shilp

Learn more

Blog

Vectors and HNSW for dummies

February 3, 2026

Vectors and HNSW for dummies

It seems like every trending startup today is talking about how they’ve built a vector search engine using HNSW. But what does that actually mean? Is HNSW some magical technology that suddenly makes search work, or is it just clever engineering? If you’ve been wondering what exactly folks are talking about when they mention Vectors and HNSW, this post is for you.

What is a Vector?

Let’s start with a thought experiment. When I say "Orange," what comes to your mind? Perhaps you think of a fruit: spherical, orange-colored, citrusy, and rich in Vitamin C. Or, you might think of the color orange. Or maybe even the global telecom company. The word "Orange" means different things based on experience and context. To help a computer understand these nuances, we need a way to represent words that captures their meanings and relationships. This is where vectors come in. Imagine we define the fruit Orange by assigning values to its features:
  • Is a fruit: Yes (1.0)
  • Color: Orange (0.8)
  • Shape: Spherical (0.9)
  • Taste: Citrus (0.7)
We can represent this as a vector (a list of numbers): [1.0, 0.8, 0.9, 0.7]. Now, let's define the color Orange:
  • Is a fruit: No (0.0)
  • Color: Orange (0.81)
  • Shape: N/A (0.0)
  • Taste: N/A (0.0)
Its vector is [0.0, 0.81, 0, 0].

Measuring Similarity

Because these are now numbers, we can use math to see how "close" they are. Using a simple formula like Euclidean Distance, we can calculate the gap between these two meanings: Distance=(1-0)2+(0.8-0.81)2+(0.9-0)2+(0.7-0)2=1.345 Now, let’s look at a Lemon: [1.0, 0.73, 0.9, 0.65]. The distance between the fruit Orange and the Lemon is only 0.086. Mathematically, the fruit Orange is much closer to a Lemon than it is to the color Orange. In the machine learning world, these vectors are called Embeddings, and the distance between them represents their Similarity. There are other ways of calculating distance like cosine similarity, Manhattan distance etc. but the core idea remains the same.

Why is Vector Representation Useful?

Vectors allow us to compare complex data points—like emotions, movies, or music—in a way that is easy for algorithms to analyze. But you might be wondering: Who chooses these specific numbers? The process of converting words or any other data into vectors is called embedding. It can also be called as encoding an object to its vector representation. In modern AI, we don't pick these numbers manually. We use Encoders (Machine Learning models).
  • Training: A model is trained on a massive dataset (like the entire internet).
  • Context: It learns that "Orange" and "Juice" often appear together, while "Orange" and "Telecom" appear in different contexts.
  • Encoding: The model learns to automatically assign numbers (embeddings) to data so that related concepts are naturally pushed closer together in a mathematical "space."
How embedding works
How embedding works
LLMs (Large Language Models) live in this embedding space. When you type a prompt, the AI converts your text into a vector, processes it through layers of math, and finds the most "similar" or logical response to return to you. We can generate embeddings for any data like text, images, audio, video etc. using appropriate embedding models. Or in another words with right kind of encoders, we can encode music, images, video into vector representations.

What is HNSW?

If you have ten vectors, searching through them is easy. But what if you have ten million? One way is brute force, calculate the distance between the embedding of the data(image/text/audio) we are searching for and all the embeddings stored. This is not efficient and will take lot of time.
In a world, compute is cheap and abundant, brute force can work (quantum computing can't solve this, so we are talking about a alien technology). Coming back to our world, we need a better way to search through large number of embeddings.
In the real world, vectors don't just have 4 dimensions; they often have 768 or 1,536. Calculating the distance between a query and every single stored vector (Brute Force) is incredibly slow. We need a shortcut. The idea is to connect similar vectors ahead of time, creating a graph. This is called a Navigable Small World (NSW). Think of it like a social network: you might not know a stranger, but you know someone who knows someone who knows them. When searching, we start near a good match, explore nearby similar items, and keep track of the best ones we’ve seen so far in case there’s something even better slightly farther away. We stop once exploring doesn’t improve the results and return the best few matches.
Small Navigable World example
Small Navigable World example
To make this even faster, we use a concept called a Skip List. Imagine a multi-story building.
  • The Top Floor (Express): Has only a few windows. You can look out and see entire cities. You jump from City A to City B in one step.
  • The Middle Floors: Have more windows. You can jump from neighborhood to neighborhood.
  • The Bottom Floor (Street Level): Every single house is visible. You walk door-to-door to find the exact address.
Hierarchical Navigable Small World
Hierarchical Navigable Small World
This is Hierarchical Navigable Small World (HNSW). It organizes vectors into layers:
  • Start at the top: Find the general "neighborhood" of your search.
  • Drop down a layer: Refine the search within that neighborhood.
  • Repeat: Keep dropping layers until you reach the bottom.
  • Final Search: Perform a tiny, local search to find the absolute best matches.
There are many optimizations and variations to the HNSW algorithm, but this is the basic idea behind how it works.

Conclusion

Vectors and HNSW are powerful concepts that enable efficient similarity search in high-dimensional spaces. Modern vector databases like Anvitra leverage these techniques to provide fast and accurate search capabilities for large-scale datasets. There are various optimizations and improvements on top of the basic implementation like PQ (Product Quantization), IVF (Inverted File Index) etc. which we will explore in future posts.