No history yet

Introduction to Vector Search

Searching by Meaning

Traditional search works by matching keywords. If you search for "blue car," you get results containing the exact words "blue" and "car." This is useful, but it has limits. What if a relevant article talks about a "navy automobile"? A keyword search would miss it completely.

Vector search is a different approach. Instead of matching exact words, it searches by meaning and context. It understands that "blue car" and "navy automobile" are conceptually similar, even though they use different words. This allows for a more intuitive and powerful way to find information.

Vector search in Elasticsearch uses vector embeddings to power modern, AI-driven search experiences.

The magic behind this is turning data—words, images, songs, and more—into numbers. Specifically, it converts them into lists of numbers called vectors. These vectors act like coordinates, placing each piece of data at a specific point in a high-dimensional space. In this space, items with similar meanings are located close together.

From Words to Vectors

How do we turn something like a word into a list of numbers? We use a machine learning model to create what's called an embedding. An embedding is a vector that captures the essence or meaning of a piece of data.

Embedding

noun

A numerical representation of data, such as text or images, in the form of a vector. This representation captures semantic relationships between data points.

Think of it like describing a color. You could represent "red" with a simple vector like (255,0,0)(255, 0, 0) in an RGB color model. "Dark red" might be (139,0,0)(139, 0, 0), and "pink" could be (255,192,203)(255, 192, 203). These vectors are numerically close because the colors are visually similar. Embeddings do the same thing for more complex concepts.

A model might learn that the words "king" and "queen" are related. When it creates embeddings, it will place their vectors close together in its conceptual space. It might also learn the relationship between "king" and "man" is similar to the one between "queen" and "woman." This allows us to perform mathematical operations on vectors to uncover relationships, like vector("king")vector("man")+vector("woman")vector("queen")vector("king") - vector("man") + vector("woman") \approx vector("queen").

Measuring Similarity

Once our data is represented as vectors, we need a way to measure how "close" they are to each other. This is where similarity metrics come in. The two most common are Euclidean distance and cosine similarity.

Euclidean distance is the straight-line distance between two points. It's what you'd measure with a ruler. A smaller distance means the items are more similar.

d(p,q)=i=1n(qipi)2d(p, q) = \sqrt{\sum_{i=1}^{n} (q_i - p_i)^2}

Imagine two vectors, A=(2,1)A = (2, 1) and B=(1,3)B = (1, 3). The distance between them is (12)2+(31)2=(1)2+22=52.24\sqrt{(1-2)^2 + (3-1)^2} = \sqrt{(-1)^2 + 2^2} = \sqrt{5} \approx 2.24. While intuitive, Euclidean distance can sometimes be misleading in high-dimensional spaces, especially when the magnitude, or length, of the vectors matters.

Cosine similarity measures the cosine of the angle between two vectors. It focuses on orientation, not magnitude. A value close to 1 means the vectors point in a similar direction, indicating high similarity. A value close to -1 means they point in opposite directions, and 0 means they are unrelated (perpendicular).

similarity=cos(θ)=ABAB=i=1nAiBii=1nAi2i=1nBi2\text{similarity} = \cos(\theta) = \frac{A \cdot B}{\|A\| \|B\|} = \frac{\sum_{i=1}^{n} A_i B_i}{\sqrt{\sum_{i=1}^{n} A_i^2} \sqrt{\sum_{i=1}^{n} B_i^2}}

Cosine similarity is often preferred for text analysis. For example, a long document and a short summary about the same topic might have vectors with very different magnitudes, making them far apart by Euclidean distance. However, their vectors would point in a similar direction, resulting in a high cosine similarity score.

Real-World Applications

Vector search isn't just a theoretical concept; it powers many of the services you use daily.

Recommendation Engines: When a streaming service suggests a new show, it's often because the vector for that show is close to the vectors of shows you've already watched.

Image Retrieval: You can search for images using another image as a query. The system converts your image into a vector and finds other images with similar vectors, effectively searching by visual content rather than tags.

Natural Language Processing (NLP): Chatbots and question-answering systems use vector search to find the most relevant documents or pre-written answers to a user's query, even if the wording is different. This is a core component of a technique called Retrieval-Augmented Generation (RAG).

Lesson image

Let's check your understanding of these core ideas.

Quiz Questions 1/5

A user searches for "canine companion" and the top result is a document about a "loyal dog". Which type of search technology is most likely being used?

Quiz Questions 2/5

In vector search, a machine learning model converts data like a word or an image into a numerical representation called a(n) ______.

Vector search provides a powerful way to find information based on its meaning, opening up new possibilities for how we interact with data.