liveBook · Manning

Cosine similarity

As explained in MongoDB 8.0 in Action, Third Edition

Cosine similarity is a measure of the cosine of the angle between two vectors, indicating their directional similarity.

Cosine similarity measures how closely two vectors point in the same direction by calculating the cosine of the angle between them. Vector databases use it to retrieve embeddings that are semantically similar to a query embedding. section 12.1.2

A typical workflow converts stored content and incoming queries into embeddings, stores the vectors, compares the query vector with stored vectors, and returns the closest matches. The metric emphasizes directional alignment rather than straight-line distance. section 12.1.2 section 12.2.3

Choose cosine similarity when:

  • Comparing text, sentences, or documents in semantic search and natural-language-processing tasks. section 12.2.3
  • Configuring an Atlas Vector Search index whose vector field contains the selected embeddings. section 12.2.3
  • Contrasting directional similarity with Euclidean distance for spatial comparisons or dot product for recommendation matching. section 12.2.3

Atlas Vector Search configuration

Define a vector field with 1,536 dimensions and set its similarity to "cosine": { "type": "vector", "path": "embedding", "numDimensions": 1536, "similarity": "cosine" }. Create the index with the Atlas CLI, then verify through mongosh that its status is READY and queryable. section 14.5.3

Figure 12.2 Vector database operations. Data is processed by an embedding model, transforming it into vector embeddings such as [1.45, ..., 1.01, 0.89, 0.06]. These embeddings are stored in a vector database. Queries from the application are also transformed into embeddings before the database is searched. The most relevant results based on these query embeddings are then retrieved and returned as query results. (Image © MongoDB 2024 CC BY-NC-SA 3.0)
A diagram of embedding model

Description automatically generated

Follow the embedding-to-query-results workflow: process content and queries into vectors, store them, search by similarity, and return the most relevant results. section 12.1.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains directional comparison and index configuration, then stops short of deriving the cosine formula or implementing the search algorithm.
Examples
Uses semantic document retrieval and MongoDB Atlas index definitions with 1,536-dimensional embedding fields.
What it assumes
Assumes embeddings, vector storage, and Atlas Vector Search workflows while defining the metric’s practical role.
What you can do after
Operate/configure it through Atlas CLI and mongosh, creating and verifying a cosine-based vector search index.
How it connects
Connects embeddings, vector databases, similarity search, Atlas Vector Search, Euclidean distance, and dot product.

Also explained in

Compare how 14 books explain cosine similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.