Cosine similarity measures the angle between two non-zero vectors. In semantic search, it compares a query embedding with document embeddings so results can match meaning beyond exact words. section 3.4 section 3.5
Scores range from -1 to 1: 1 indicates the same direction, -1 the opposite direction, and 0 orthogonality or decorrelation. Higher similarity scores indicate more closely related embeddings; SciPy's cosine distance reverses this ordering because smaller distances indicate greater similarity. section 3.4
A practical semantic-search implementation follows this sequence:
- Encode each document into a dense vector with an embedding model. section 3.4
- Encode the incoming query into a vector in the same space. section 3.4
- Compute cosine similarity or cosine distance between the query and every document embedding. section 3.4
- Rank documents by highest similarity, or equivalently by lowest cosine distance. section 3.4
Toy semantic search
For the query “Machine learning,” six example sentences receive scores including 0.6723 for “Machine learning is based on algorithms,” 0.4380 for “Deep learning uses neural networks,” and 0.3609 for “Recurrent networks have connections.” The highest-scoring document is returned. section 3.4
Figure 3.4 A graphical demonstration of cosine similarity using two dimensions ("pet" and "furry"). Three words are drawn as vectors into the coordinate system. θ 1 denotes the angle between bird and dog, θ 2 depicts the angle between dog and cat, respectively. The smaller angle θ 2 illustrates a higher similarity of dogs with cats compared to birds.’
Visualizes cosine similarity as the angle between vectors: the smaller angle between dog and cat represents greater similarity than the larger angle between bird and dog. section 3.4
























