Elasticsearch for RAG Vector Storage
Introduction to Vector Search
From Words to Numbers
Traditional search is great at finding exact words. If you search for "corgi," you get documents with the word "corgi." But what if a relevant article uses the word "dog" or "Welsh herding canine" instead? Keyword search would miss it entirely.
Vector search solves this by understanding meaning, not just words. It starts by converting data—text, images, audio—into numerical representations called vectors, or embeddings. You can think of a vector as a list of numbers that captures the essence of a piece of data.
Imagine a giant map where every concept has a coordinate. Concepts with similar meanings, like "king" and "queen," are placed close together. Concepts that are far apart, like "king" and "cabbage," are unrelated.
An AI model, called an embedding model, is trained on vast amounts of data to learn these relationships. When you give it a word, sentence, or even an entire document, it plots it onto this conceptual map by generating a vector. This process turns your unstructured data into something a computer can mathematically compare.
Measuring Closeness
Once we have these vectors, searching becomes a matter of finding the "closest" vectors to your query. In this vector space, closeness equals similarity. But how do we measure it? There are a few common ways.
Euclidean Distance
adjective
Measures the straight-line distance between the endpoints of two vectors.
This is the classic "as the crow flies" distance you learned in geometry. It's simple and effective for many use cases. A smaller distance means the items are more similar.
Cosine Similarity
noun
Measures the cosine of the angle between two vectors, indicating if they point in the same direction.
Instead of distance, this measures orientation. Are two vectors pointing in roughly the same direction? If they are, the angle between them is small, and their cosine similarity is high (close to 1). If they point in opposite directions, the similarity is low (close to -1). This is useful for text, where a short document and a long essay about the same topic should be considered similar.
Dot Product
noun
Calculates the product of the magnitudes of two vectors and the cosine of the angle between them.
The dot product is related to cosine similarity but also takes the length, or magnitude, of the vectors into account. A larger dot product generally indicates greater similarity. It's computationally efficient, which makes it a popular choice.
The Challenge of Many Dimensions
The examples above are in two dimensions, which are easy to visualize. Real-world embeddings, however, don't live on a flat plane. They exist in spaces with hundreds or even thousands of dimensions.
This creates a strange and counterintuitive environment. As you add more dimensions, the volume of the space increases exponentially. It's a phenomenon often called the "curse of dimensionality."
Imagine searching for your friend. If they are on a single path (one dimension), it's easy. If they are in an open field (two dimensions), it's harder. If they are in a 1,000-story skyscraper with a huge floor plan (three dimensions), it's very difficult. Now, imagine a space with 1,000 dimensions.
In such a vast space, everything tends to be far apart from everything else. The concept of "nearest neighbor" becomes less meaningful because many points might have similarly large distances from your query vector. Calculating distances across so many dimensions is also computationally expensive. This is why vector search relies on specialized databases and clever algorithms to find approximate nearest neighbors quickly, rather than calculating the exact distance to every single point.
