Lesson illustration

Vector Similarity Search for Information Retrieval

Welcome to our third lesson in the module on long-term memory. In our last session, you gained practical experience generating text embeddings with the OpenAI API, turning raw text like product descriptions into meaningful numerical vectors. You now have the "data," but the crucial question remains: how do we use this data to find relevant information?

This lesson answers that question. Our objective is to explain the concept of vector similarity search for information retrieval. We will explore the core principle of finding "nearby" vectors in a high-dimensional space and the mathematical tools, or distance metrics, used to measure this proximity. Understanding this concept is fundamental to building the memory for our autonomous agents, enabling them to search vast knowledge bases—like an entire e-commerce catalog—based on semantic meaning rather than just keywords.

From Keywords to Concepts: The Idea of Semantic Search

Traditional search, like using Ctrl+F in a document or a basic SQL LIKE query, relies on exact keyword matches. This approach is brittle; a search for "men's leather billfold" would miss a product titled "classic bifold wallet for him." Semantic search overcomes this by operating on the meaning of the query, not just the words.

Since embeddings capture the semantic essence of text, we can rephrase our search problem: "Find documents with similar meaning" becomes "Find vectors that are close to my query vector."

{"type":"image","url":"https://weaviate.io/assets/images/vector-search-6dee9d7ee1ecbc7de37e118c8731476c.png","caption":"This image illustrates how a query for \"Kitten\" retrieves semantically related concepts like \"Cat,\" \"Dog,\" and \"Wolf.\" These items are grouped together in the vector space, demonstrating that the search finds neighbors based on meaning, not just term matches.","isV2":true,"blockId":"f3828493-d6f3-4b57-918f-ee23a8f2b296","lessonId":"7dd71788-a1f6-4621-8c63-2919e9520f8f"}



This process of finding the closest vectors is the heart of vector similarity search. The following article introduces this idea and the underlying principle.

{"type":"reading","par_intro":"This article provides a concise introduction to semantic search, contrasting it with keyword search and explaining the core principle of finding nearby vectors.","par_directions":"Please read the first three sections of the article:\n1. <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"e2f9dfe3\" data-range-start=\"What is Semantic Search?\" data-range-end=\"the underlying meaning of the content.\">What is Semantic Search?</span>\n2. <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"ecfd72d6\" data-range-start=\"Understanding Vector Data Distribution\" data-range-end=\"by finding nearby vectors in this space.\">Understanding Vector Data Distribution</span>, focusing on the idea of semantic clustering.\n3. <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"9a5e2d70\" data-range-start=\"How Similarity Search Works\" data-range-end=\"semantically similar items appearing first.\">How Similarity Search Works</span>, which introduces the concept of k-Nearest Neighbors (k-NN).","learning_duration":"5 minutes","url":"https://medium.com/@derrickryangiggs/understanding-semantic-search-vector-embeddings-and-similarity-search-422bcb4a495b","title":"Understanding Semantic Search: Vector Embeddings and ...","isV2":true,"blockId":"5978fb09-e92e-41ec-a781-89d1a984449d","lessonId":"7dd71788-a1f6-4621-8c63-2919e9520f8f"}



As the article explains, the fundamental task is a k-Nearest Neighbors (k-NN) search. Given a query vector and a collection of other vectors, we want to find the 'k' vectors that are "closest" to our query. But how do we mathematically define "closest"?

{
  "type": "exercise",
  "id": "c3a1c028-51f6-4ed2-b660-bcf0b264e762"
}

Measuring Closeness: Distance and Similarity Metrics

To compare vectors, we need a formal way to measure the distance or similarity between them. Several mathematical functions, known as distance metrics, serve this purpose. Each has different properties and is suited for different kinds of data.

{"type":"image","url":"https://weaviate.io/assets/images/hero-88853b1677e0e7c29494a1f2c2b35c66.png","caption":"This chart displays four common metrics for vector search: Cosine Distance, Squared Euclidean, Dot Product, and Manhattan. Each metric provides a different mathematical approach to calculating the similarity or distance between two vectors.","isV2":true,"blockId":"1297d13c-51b5-4ff9-a4ab-8848f9861b09","lessonId":"7dd71788-a1f6-4621-8c63-2919e9520f8f"}



For text embeddings generated by LLMs, the most important and widely used metric is Cosine Similarity.

Cosine Similarity: Measuring the Angle

Instead of measuring the straight-line distance between vector endpoints (like Euclidean distance), cosine similarity measures the angle between two vectors. A smaller angle implies higher similarity.

Why is this so effective for text? Imagine two product reviews:

  1. "Great product!"
  2. "Great product! I love it, it's a fantastic item and I would recommend it to everyone."

The second review is much longer, so its embedding vector might have a larger magnitude (length). However, the core sentiment and meaning are identical. Cosine similarity ignores the vector's magnitude and focuses only on its direction (the angle), correctly identifying these two reviews as being highly similar.

The following video from StatQuest provides an excellent and intuitive explanation of how cosine similarity works.

{"type":"video","title":"Cosine Similarity, Clearly Explained!!!","learning_duration":499,"video_id":"e9U0QAFbfLI","par_intro":"This video breaks down the concept of cosine similarity using simple, visual examples, moving from graphical intuition to the mathematical formula.","par_directions":"Please watch the following segments:\n*   <span data-type=\"resource_video_timerange\" data-resource-subitem-id=\"8c6d44dd\" data-range-start=\"46\" data-range-end=\"102\">Introduction</span>: Sets up the problem of measuring text similarity.\n*   <span data-type=\"resource_video_timerange\" data-resource-subitem-id=\"eee70cf1\" data-range-start=\"102\" data-range-end=\"224\">Graphical Intuition</span>: Shows how phrases are converted to vectors and how the angle between them represents similarity.\n*   <span data-type=\"resource_video_timerange\" data-resource-subitem-id=\"58d0daa0\" data-range-start=\"224\" data-range-end=\"270\">Magnitude vs. Angle</span>: This is a key part that clarifies why vector length doesn't affect cosine similarity.\n*   <span data-type=\"resource_video_timerange\" data-resource-subitem-id=\"86212213\" data-range-start=\"270\" data-range-end=\"358\">Interpreting the Score</span>: Explains the meaning of scores from 0 (dissimilar) to 1 (identical).\n*   <span data-type=\"resource_video_timerange\" data-resource-subitem-id=\"43e9a4c1\" data-range-start=\"358\" data-range-end=\"545\">The Formula</span>: Introduces the mathematical formula used for calculation, which is essential for implementation.","video_duration":613,"isV2":true,"blockId":"29cb6a0a-d0fe-4872-8514-de98fb5aedc2","lessonId":"7dd71788-a1f6-4621-8c63-2919e9520f8f"}



As the video explains, cosine similarity values range from -1 to 1, but since modern embedding models like OpenAI's produce vectors where all values are positive, the practical range is 0 to 1.

  • 1: The vectors point in the exact same direction (identical meaning).
  • 0: The vectors are orthogonal (90-degree angle), indicating no semantic relationship.

Often, you will see "Cosine Distance" mentioned, which is simply calculated as 1 - Cosine Similarity. In this case, a distance of 0 means the items are identical.

{
  "type": "exercise",
  "id": "e84bc02d-3ff8-4288-b133-ca2e80815047"
}

Other Common Metrics: Euclidean and Dot Product

While cosine similarity is dominant for text, it's helpful to be aware of other metrics:

  • Euclidean Distance (L2 Norm): This is the straight-line, "as the crow flies" distance between the tips of two vectors. It considers both magnitude and direction. It's very intuitive but can be less effective for text where document length (magnitude) can vary without changing the core topic.
  • Dot Product: This calculates similarity by multiplying the corresponding components of two vectors and summing the results. It's extremely fast to compute. For vectors that have been normalized (scaled so their length is 1), the dot product produces the same similarity ranking as cosine similarity. Since OpenAI's embedding models return normalized vectors, using the dot product can be a performance optimization.

The following article offers a deeper comparison of these metrics, which will build on your conceptual foundation.

{"type":"reading","par_intro":"This article dives into the mathematical details and practical trade-offs between the three main similarity metrics used in vector search.","par_directions":"Please read the following sections to understand the nuances:\n1.  Start with <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"3aa688fd\" data-range-start=\"The Euclidean distance\" data-range-end=\"L1 is simply |x| + |y|. How?\">The Euclidean distance</span> to get a formal definition.\n2.  Read <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"b46208b7\" data-range-start=\"The vector Dot Product\" data-range-end=\"the cosine distance.\">The vector Dot Product</span> to understand its derivation and properties.\n3.  Continue to <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"85243629\" data-range-start=\"The cosine distance\" data-range-end=\"ignoring magnitudes.\">The cosine distance</span>, which formally defines the metric we just discussed.\n4.  Then, review the section on the relation between these metrics. The key takeaway is how they relate for normalized vectors.\n5.  Finally, read the crucial section <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"57e71feb\" data-range-start=\"Choosing between Cosine or Euclidean Distance or Dot Product\" data-range-end=\"discriminative power of the embeddings generated.\">Choosing between Cosine or Euclidean</span>. Pay close attention to the discussion on normalized vs. non-normalized vectors and when each metric is preferable.","learning_duration":"15 minutes","url":"https://medium.com/data-science-collective/cosine-distance-vs-dot-product-vs-euclidean-in-vector-similarity-search-227a6db32edb","title":"Cosine Distance vs Dot Product vs Euclidean in vector ...","isV2":true,"blockId":"ea936392-eeed-4b47-8a7d-29deb49aab9a","lessonId":"7dd71788-a1f6-4621-8c63-2919e9520f8f"}



The Scalability Challenge: Exact vs. Approximate Search

Now you know how to calculate the distance between a query vector and one other vector. But what if your e-commerce database has 10 million product embeddings? Calculating the cosine similarity between your query and all 10 million other vectors would be incredibly slow. This brute-force approach is called Exact Search or Exhaustive Search.

While it guarantees finding the absolute closest neighbors, it's not practical for real-time applications at scale. This is where the most critical concept for production vector search comes in: Approximate Nearest Neighbor (ANN) search.

ANN algorithms create clever data structures (indexes) that allow for much faster searching by intelligently narrowing down the search space. They trade a tiny amount of accuracy (e.g., finding a result that is 99% as good as the perfect match) for a massive speed-up—often turning a search that would take hours into one that takes milliseconds.

Given your background, you can think of this as analogous to database indexing. An exact search is like a full table scan, while an ANN search is like using a B-tree index to quickly find the relevant rows without scanning the whole table.

The following video and article explain this trade-off clearly.

{"type":"video","title":"What is a Vector Database? Powering Semantic Search & AI Applications","learning_duration":76,"video_id":"gl1r1XV0SLw","par_intro":"This segment from an IBM Technology video introduces the scalability problem and the role of vector indexing and ANN algorithms.","par_directions":"Please watch from <span data-type=\"resource_video_timerange\" data-resource-subitem-id=\"2d10b00c\" data-range-start=\"454\" data-range-end=\"530\">the discussion on similarity search</span>. The speaker explains why an exhaustive search is too slow and introduces vector indexing with ANN algorithms like HNSW and IVF as the solution.","video_duration":588,"isV2":true,"blockId":"89185b13-3312-4d0c-bbac-a1c7ec05407f","lessonId":"7dd71788-a1f6-4621-8c63-2919e9520f8f"}



Now, let's solidify this with a more detailed comparison.

{"type":"reading","par_intro":"This part of the article we looked at earlier provides a clear, practical comparison between Exact and Approximate search methods.","par_directions":"Please read the final two sections of the article:\n*   Types of Similarity Search, which describes Exact and Approximate (ANN) search, their characteristics, and popular ANN algorithms.\n*   <span data-type=\"resource_reading_textrange\" data-resource-subitem-id=\"a9bd29c5\" data-range-start=\"Comparing the Two Approaches\" data-range-end=\"the clear choice.\">Comparing the Two Approaches</span>, which provides a helpful summary table and a real-world example of the performance difference.","learning_duration":"5 minutes","url":"https://medium.com/@derrickryangiggs/understanding-semantic-search-vector-embeddings-and-similarity-search-422bcb4a495b","title":"Understanding Semantic Search: Vector Embeddings and ...","isV2":true,"blockId":"e73beee9-5543-4f3e-8b09-bf2e627fc7b7","lessonId":"7dd71788-a1f6-4621-8c63-2919e9520f8f"}



For nearly all business applications you'll build, especially those involving large datasets like product catalogs or customer support logs, ANN search will be the default choice.

{
  "type": "exercise",
  "id": "869fd19d-d49c-478f-9c70-174c69f38211"
}

Conclusion

In this lesson, we have demystified the core mechanism behind an agent's long-term memory: vector similarity search. You've moved from having a collection of abstract vectors to understanding how to query them for meaning.

Here are the key takeaways:

  • Vector similarity search is the process of finding the "nearest" vectors to a query vector in an embedding space, which corresponds to finding semantically similar information.
  • The "nearness" is calculated using distance metrics. Cosine Similarity, which measures the angle between vectors, is the standard for text-based semantic search because it is insensitive to document length.
  • For normalized vectors, like those from OpenAI, the Dot Product offers a faster computation that yields the same rankings as Cosine Similarity.
  • Brute-force Exact Search is too slow for large datasets. The practical solution is Approximate Nearest Neighbor (ANN) search, which uses specialized indexes to provide ultra-fast results with a negligible trade-off in accuracy.

You now have the complete conceptual picture: converting text to embeddings (Lesson 5.2) and searching those embeddings for similarity (this lesson). In our next lesson, we will make this concrete. You will learn how to set up a vector index on a managed service like Qdrant and perform create, read, update, and delete (CRUD) operations, building the first practical component of your agent's persistent memory.

Can't find a good explanation? Sign up and we'll make it for you