Cosine distance
Cosine distance is a measure of dissimilarity between nonzero vectors based on their angular separation, commonly defined as one minus cosine similarity.
Just Use Postgres!
Cosine distance is a dissimilarity measure derived from the angle between two vectors, commonly defined as one minus cosine similarity.
- How it is used
- Ranks semantically related embeddings for movie recommendations and vector similarity search in Postgres.
- How deep it goes
- Explains angle-based ranking, SQL execution, distance values, phrase refinement, and HNSW indexing without developing the underlying mathematics.
- Examples
- Uses Star Wars movie searches, explicit distance output, refined phrases, SQL queries, and HNSW execution plans.
- What you can do after
- Apply cosine distance via pgvector SQL to rank embeddings, inspect relevance, refine queries, and configure HNSW search.
Natural Language Processing in Action, Second Edition
The cosine distance metric measures the angular dissimilarity between vectors and is commonly used to estimate similarity in their encoded meanings.
- How it is used
- Estimates semantic similarity between natural-language vectors and supports efficient comparison in search and language-processing systems.
- How deep it goes
- Explains angular meaning, bounded scaling, Euclidean contrast, and similarity conversion, but does not derive the full formula.
- Examples
- Uses message-quality vectors, a two-dimensional NumPy subtraction, and a diagram of Euclidean vector distance.
- What you can do after
- Apply cosine distance to compare high-dimensional language vectors and convert it into cosine similarity when needed.
Read how Natural Language Processing in Action, Second Edition explains cosine distance →
Software Testing with Generative AI
A distance measure derived from the cosine of the angle between vectors, where a lower value indicates greater directional similarity.
- How it is used
- Ranks user-story documents against queries to retrieve context for testing-risk prompts.
- How deep it goes
- Demonstrates implementation and selection logic but defers the mathematical workings and production-level algorithm complexity.
- Examples
- Uses user stories, document corpora, the query “I want test ideas,” and a lowest-score Java matching method.
- What you can do after
- Apply it via Apache Commons Text to select the closest document for a generated prompt.
Read how Software Testing with Generative AI explains cosine distance →
The Art of AI Product Development
Cosine distance measures the difference in orientation between two vectors and is commonly used to compare embeddings.
- How it is used
- Uses cosine distance to compare query and document embeddings during semantic search and retrieval-augmented generation.
- How deep it goes
- Explains query embedding, similarity-based top-k retrieval, tuning trade-offs, and information loss without deriving the mathematics.
- Examples
- Uses embedded queries, stored text chunks, top-k results, and Weaviate retrieval as concrete examples.
- What you can do after
- Apply it via an embedding database while tuning retrieval breadth and evaluating relevance problems.
Read how The Art of AI Product Development explains cosine distance →
AI Agents in Action
Cosine distance is a vector distance measure defined as one minus cosine similarity, ranging from zero for most similar vectors to two for semantically opposite vectors.
- How it is used
- Supports document similarity comparison and semantic retrieval in vector-search workflows.
- How deep it goes
- Defines the equation, range, and score interpretation, then demonstrates TF–IDF and embedding-based implementations without covering more advanced similarity measures.
- Examples
- Uses eight sample documents, a similarity matrix, interactive document selection, and a Chroma embedding query.
- What you can do after
- Operate scikit-learn and Chroma workflows that compare vectors or retrieve documents using distance scores.
AI Agents in Action, Second Edition
Cosine distance is a metric equal to one minus cosine similarity that measures angular dissimilarity between vectors.
- How it is used
- Ranks semantically retrieved documents by angular dissimilarity instead of relying only on matching terms or phrases.
- How deep it goes
- Defines the one-minus-similarity formula, explains the 0-to-2 range, and connects scores to vector retrieval without deriving implementation internals.
- Examples
- Uses TF-IDF document vectors, sentence-pair similarity matrices, and Chroma queries returning document identifiers, distances, and text.
- What you can do after
- Apply it via Chroma DB or cosine-based vector search to interpret and rank document-retrieval results.
Read how AI Agents in Action, Second Edition explains cosine distance →
Related concepts
- cosine similarityequivalent to · Cosine similarity measures the directional alignment of two nonzero vectors as the cosine of the angle between them, equivalently their dot product divided by the product of their magnitudes.
- Euclidean distancealternative to · Euclidean distance is the straight-line distance between two points in Euclidean space, calculated as the square root of the sum of squared differences between corresponding coordinates.
- followed by operatorkind of · The PostgreSQL full-text search followed-by operator matches lexemes occurring in the specified positional sequence.
- Apache Commons Textused by · A Java library providing utilities for text processing, comparison, escaping, and related operations.
- similarity metricproduces · A similarity measure is a rule that quantifies or characterizes how alike two objects or representations are according to selected properties.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.





